can you guys pls help me
i want to code where the user will enter a number and then using for loop it will display the number in decrement for example:
Enter a number: 4
OUTPUT:
4
4-3
4-3-2
4-3-2-1
3
3-2
3-2-1
2
2-1
1-1
code it using for loop
this is my attempt code i’m getting confused:
for (int i = num; i >= 1; i--) {
for (int j = 1; j <= i; j++)
{
System.out.print(i+ "-" );
for (int k = num; k>=i; k--)
{
System.out.print(k);
}
System.out.println();
}
>Solution :
I can’t produce your last line(1-1) from the pattern with the below code, as if you think doesn’t match the pattern as well rather than that it can produce the exact same results:
for (int x = n; x >= 1; x--) {
for (int i = n; i >= 1; i--) {
for (int j = n; j >= i; j--) {
System.out.print (j);
if (j > i) {
System.out.print("-");
}
}
System.out.println();
}
n--;
}