Skip to content
Advertisement

How to print number 1 to 10(in that sequence only)using loops(java) if int i = 10

Output must be 1 2 3 4 5 6 7 8 9 10

I have tried While loop but it prints from 10 to 1

Answer

Assuming you must use i with an initial value of 10.

for(int i = 10; i > 0; i--) {
    System.out.println(11-i);
}

// or using a while loop
int i = 10;
while(i > 0) {
    System.out.println(11-i--);
}
Advertisement