File tree 1 file changed +44
-0
lines changed
InterviewPrograms/src/com/java/patterns
1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .java .patterns ;
2
+ import java .util .Scanner ;
3
+ /*
4
+ Write the program to print the following pattern
5
+ 0
6
+ 1 0 1
7
+ 2 1 0 1 2
8
+ 3 2 1 0 1 2 3
9
+ 4 3 2 1 0 1 2 3 4
10
+ */
11
+ public class Pattern16 {
12
+ public static void main (String args [] ) throws Exception {
13
+ Scanner scanner = new Scanner (System .in );
14
+ int N = Integer .parseInt (scanner .nextLine ().trim ());
15
+ int d = (N -1 )*2 ;
16
+ for (int i =0 ;i <N ;i ++){
17
+ for (int j =0 ;j <d -1 ;j ++)
18
+ System .out .print (" " );
19
+ for (int j =i ;j >=0 ;j --)
20
+ if (i == N -1 && j == i )
21
+ System .out .print (j );
22
+ else
23
+ System .out .print (" " +j );
24
+ for (int j =1 ;j <=i ;j ++)
25
+ System .out .print (" " +j );
26
+ if (i ==0 && N != 1 )
27
+ System .out .print (" " );
28
+ d -=2 ;
29
+ if (i < N -1 )
30
+ System .out .println ();
31
+ }
32
+ scanner .close ();
33
+ }
34
+ }
35
+ /*
36
+ Input
37
+ 5
38
+ Output
39
+ 0
40
+ 1 0 1
41
+ 2 1 0 1 2
42
+ 3 2 1 0 1 2 3
43
+ 4 3 2 1 0 1 2 3 4
44
+ */
You can’t perform that action at this time.
0 commit comments