Ris
Ris

Reputation: 1912

how to do this with for loop

I am trying to practice for loops and I cannot get the for loop to do this for me, how can I have the code that would give me this result ?

1 2 3 4 5 6 7 8 9 10
3
4
5
6
7
8
9
10

I know simple for loop

    for(int y = 1; y <= 10; y++){




        System.out.println(y);
        }

result

1
2
3
4
5
6
7
8
9
10

Upvotes: 0

Views: 46

Answers (2)

Elliott Frisch
Elliott Frisch

Reputation: 201409

You could do this

for (int i = 1; i < 11; i++) {
  System.out.print(i);
  if (i == 1) {
    for (int t = i + 1; t < 11; t++) {
      System.out.print(" " + t);
    }
  }
  System.out.println();
}

Upvotes: 1

Christian Tapia
Christian Tapia

Reputation: 34146

Try this:

    for (int y = 1; y <= 10; y++) {

        System.out.print(y + " ");
    }
    System.out.println();
    for (int y = 3; y <= 10; y++) {

        System.out.println(y);
    }

Upvotes: 2

Related Questions