Description: This program prints the following pattern:
1 2 3 4 |
* ** *** **** |
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 |
//Primary Input : Generates a single digit number (n) representing the number of lines /* Primary Output: The following pattern is generated for n lines * ** *** **** */ class Pattern5 { 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 stars up to current line number for (int j = 1; j <= i; j++) System.out.print("*"); // Move to the next line System.out.println(); } } // end of main } |