File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
InterviewPrograms/src/com/java/patterns Expand file tree Collapse file tree 1 file changed +40
-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
+ 1
6
+ 1 2 3
7
+ 1 2 3 4 5
8
+ 1 2 3 4 5 6 7
9
+ 1 2 3 4 5 6 7 8 9
10
+ */
11
+ public class Pattern20 {
12
+ public static void main (String args [] ) throws Exception {
13
+ Scanner scanner = new Scanner (System .in );
14
+ int N = Integer .parseInt (scanner .nextLine ());
15
+ int iSpace = 0 ;
16
+ for (int i =0 ;i <N ;i ++){
17
+ for (int j = iSpace ;j <N -1 ;j ++)
18
+ System .out .print (" " );
19
+ for (int j =1 ;j <=2 *i +1 ;j ++)
20
+ if (j == 1 )
21
+ System .out .print (j );
22
+ else
23
+ System .out .print (" " +j );
24
+ iSpace ++;
25
+ if (i != N -1 )
26
+ System .out .println ();
27
+ }
28
+ scanner .close ();
29
+ }
30
+ }
31
+ /*
32
+ Input
33
+ 5
34
+ Output
35
+ 1
36
+ 1 2 3
37
+ 1 2 3 4 5
38
+ 1 2 3 4 5 6 7
39
+ 1 2 3 4 5 6 7 8 9
40
+ */
You can’t perform that action at this time.
0 commit comments