Advertisement
Shailrshah

PattenOne

Jul 28th, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 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; i++){
  19.                 for(j = 1; j <= i; j++) System.out.print(j);
  20.                 for(j = 0; j < (n*2-i*2); j++) System.out.print(" ");
  21.                 for(j = i; j > 0; j--) System.out.print(j);
  22.                 System.out.println();
  23.         }
  24.     }
  25. }
  26. //Output:-
  27. // Enter the number of rows:  6
  28. // 1          1
  29. // 12        21
  30. // 123      321
  31. // 1234    4321
  32. // 12345  54321
  33. // 123456654321
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement