Advertisement
Shekhar777

Java Loops and Introduction to Arrays - Post Class - Pattern making

Oct 19th, 2020
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1. Q16-Given an integer n, your task is to print the pattern as shown in example:-
  2. For n=5, the pattern is:
  3. 1
  4. 1 2 1
  5. 1 2 3 2 1
  6. 1 2 3 4 3 2 1
  7. 1 2 3 4 5 4 3 2 1
  8. 1 2 3 4 3 2 1
  9. 1 2 3 2 1
  10. 1 2 1
  11. 1
  12. Ans-public static void pattern_making(int n){
  13.            for(int i=1; i<=n; i++){
  14.                for(int j=1; j<=i; j++){
  15.                    System.out.print(j+" ");
  16.                }
  17.                for(int k=i-1; k>0; k--){
  18.                    System.out.print(k+" ");
  19.                }
  20.                System.out.println();
  21.            }
  22.            for(int i=n-1; i>0; i--){
  23.                for(int j=1; j<i; j++){
  24.                    System.out.print(j+" ");
  25.                }
  26.                for(int k=i; k>0; k--){
  27.                    System.out.print(k+" ");
  28.                }
  29.                System.out.println();
  30.            }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement