Sonu Sonu - 1 month ago 9
Java Question

print n number in specific format

I want to print n number in this format :

5
5 4
5 4 3
5 4 3 2
5 4 3 2 1


but when I tried to print using is code,
I got only

5
5 4
5 4 3
5 4 3 2


I didn't get

5 4 3 2 1


I don't know what's wrong in my code.

for(int i=1; i<n; i++){
for(int j=n; j>n-i ;j--){
System.out.print(j);
}
System.out.println();
}

Answer

Your for loop for j can represent the x axis, and the i can represent the y axis. When you input n, it should be one more for the y axis because you are missing the last step. So it should be changed to

for(int i=1; i <=n; i++)