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

Time:03-24

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

CodePudding user response:

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--);
}

CodePudding user response:

You can use a for loop like the following:

for(int i = 1; i <= 10; i  ) {
   System.out.println(i);
}
  • Related