Skip to content

Instantly share code, notes, and snippets.

@gitaficionado
Created November 29, 2019 19:59
Show Gist options
  • Save gitaficionado/6902bbabb385af2c38d3330e201b001d to your computer and use it in GitHub Desktop.
Save gitaficionado/6902bbabb385af2c38d3330e201b001d to your computer and use it in GitHub Desktop.
Write a method to display a pattern similar that on page 216 in the Liang textbook. The method header ispublic static void displayPattern(int n)
/**
*(Display patterns) Write a method to display a pattern as follows:
*1
*2 1
*3 2 1
*...
*n n-1 ... 3 2 1
*The method header is
*public static void displayPattern(int n)
*/
public class DisplayPatterns_06_06 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter line number: ");
int lineNumber = input.nextInt();
displayPattern(lineNumber);
}
public static void displayPattern(int n) {
for (int row = 1; row <= n; row++) {
// Print spaces
for (int i = row; i < n; i++)
System.out.print(" ");
// Print numbers
for (int i = row; i >= 1; i--)
System.out.print(" " + i);
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment