Advertisement
Shailrshah

Pascal Triangle (Stars)

Jul 28th, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.io.IOException;
  4. class PatternOne{
  5.     static int getInput(String prompt){
  6.         System.out.print(prompt);
  7.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  8.         while(true){
  9.             try{
  10.                 return Integer.parseInt(br.readLine());
  11.             } catch(IOException | NumberFormatException nfe){
  12.                 System.out.print("Exception in input! Try again: ");
  13.             }
  14.         }
  15.     }
  16.     public static void main(String args[]){
  17.         int i, j, n = getInput("Enter the number of rows:  ");
  18.         for(i = 1; i <= ((n+1)/2); i++){
  19.                 for(j = 0; j <((n+1)/2)-i ; j++) System.out.print(" ");
  20.                 for(j = 0; j < i; j++) System.out.print("* ");
  21.                 System.out.println();
  22.         }
  23.         for(i = 1; i < ((n+1)/2); i++){
  24.             for(j = 0; j < i; j++) System.out.print(" ");
  25.             for(j = 0; j < ((n+1)/2)-i; j++) System.out.print("* ");
  26.             System.out.println();
  27.         }
  28.     }
  29. }
  30. /*Output:-
  31. Enter the number of rows:  9
  32.     *
  33.    * *
  34.   * * *
  35.  * * * *
  36. * * * * *
  37.  * * * *
  38.   * * *
  39.    * *
  40.     *
  41. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement