Description: This program prints the following pattern:
1
121
12321
1234321
123454321
121
12321
1234321
123454321
Primary Inputs: Generates a single digit random number
Primary Output: The pattern specified above
Platform Used: JDK 1.6 with JCreator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
//Primary Input : Generates a single digit number (n) representing the number of lines /* Primary Output: The following pattern is generated for n lines 1 121 12321 1234321 123454321 */ class Pattern6 { public static void main(String args[]) { int n; n = (int) (Math.random() * 10); System.out.println("The number of lines are: " + n); for (int i = 1; i <= n; i++) { int count = n - i; // count of initial blank spaces for each line // Loop for printing the blank space at the beginning of line for (int j = 1; j <= count; j++) System.out.print(" "); // Print numbers up to current line number for (int j = 1; j <= i; j++) System.out.print(j); // Print numbers up to one less than current line number for (int j = i - 1; j >= 1; j--) System.out.print(j); // Move to the next line System.out.println(); } } // end of main } |