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