Java while loop print the integers from 1 to 20

Question

We would like to use while loop print the integers from 1 to 20.

Output should be like this:

1     2     3     4     5
6     7     8     9     10
11    12    13    14    15
16    17    18    19    20


public class Main {
  public static void main(String[] args) {
    int i = 1;

    while (i <= 20){
      System.out.print(i);

      if (i % 5 == 0)
        System.out.println();
      else
        System.out.print('\t');
      ++i;
    }
  }
}



PreviousNext

Related