Home > Net >  how do i print by twos in java?
how do i print by twos in java?

Time:04-09

Code

int Y;

System.out.print("Enter num1: ");
Y = Integer.parseInt(br.readLine());

for (int X = 0; X < Y; X  ){
    System.out.println(X); 
}

current output:

0
1
2
3
4
5

desired output:

0 1
2 3
4 5 

CodePudding user response:

Here is how you can add small check and println once condition met on X

    int Y=5;
    for (int X = 0; X <= Y; X  ){
         System.out.print(X " ");
        if(X%2!=0)// Add a new line only if condition met i.e on odds
            System.out.println();
    }

output

 0 1
 2 3
 4 5
  • Related