+ 2

[SOLVED] Write a program to print the following pattern in java.

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

1st Dec 2017, 1:57 AM
Gammaburst
Gammaburst - avatar
4 Respuestas
+ 1
try this.. :) int b=0; int index=15; for(int y = 1; y < 6 ; y++) { for(int x = 0; x < y ; x++) { if(b==x) { for(int z=y;z<6;z++) { if(index<10) { System.out.print(" " + index); } else { System.out.print(" " + index); } index--; } b++; } else{ System.out.print(" "); } } System.out.println(); }
2nd Dec 2017, 5:35 AM
elle👑
elle👑 - avatar
+ 1
int index=15; int b=0; for(int y = 1; y < 6 ; y++) { for(int x = 0; x < y ; x++) { if(b==x) { for(int z=y;z<=5;z++) { System.out.print(index); index--; } b++; } System.out.print(" "); } System.out.println(); }
1st Dec 2017, 2:01 AM
elle👑
elle👑 - avatar
+ 1
Thank you elle but still, if you run this code, you will see that there is a problem of spaces. The output is: 1514131211 10987 654 32 1 Can you please solve it?
2nd Dec 2017, 3:17 AM
Gammaburst
Gammaburst - avatar
+ 1
Great! It was better than before and I tweaked it just a little bit. Added another empty space in the 'if' print statement and two empty spaces to the second last print statement which prints empty spaces before the numbers. Now, it runs perfectly! Thanks from the bottom of my heart for your effort. Have a great day. :D
3rd Dec 2017, 1:13 AM
Gammaburst
Gammaburst - avatar