Why do even numbers do not have the same output as odd numbers?

1

I made a program that asks the user for a number that will serve as maximum value. And then the program shows the odd and even numbers from 0 to that number. I used printf to have an output like this:

Os números pares são os seguintes:
2   4   6   8  10

But I have the following output:

Os números pares são os seguintes:

2

4

6

8

10

I do not understand why, since in odd numbers the output is intended but in pairs it is not. Here's the code I used:

{
    String repetir = " " ;
    do
    {
        System.out.println("Indique um valor máximo");
        int max = scanner.nextInt();
        int par= 0;
        int impar = 0;

        System . out . println("Os números pares são os seguintes:");
        while(par<= max)
        {

            if (par%2 == 0)
            {
                System . out . printf ("%3d",par); 
                System.out.println("   ");
            }
            par++;
        }
        System . out . println("Os números impares são os seguintes:");
        while (impar <= max)
        {
            if (impar %2 != 0)
            {
                System . out .printf ("%3d",impar); 
            }
            impar++;
        }
        System.out.println("   ");
        System.out.println("Deseja repetir a operação?(s/n)");
        repetir = scanner.next();
    }while (repetir.equals("s"));
}
    
asked by anonymous 28.10.2017 / 11:48

2 answers

5

Just remove the line

System.out.println("   ");

Because you print an empty line after each even number

if (par%2 == 0)
{
    System . out . printf ("%3d",par); 
    System.out.println("   "); // <--------
}
par++;

And the odd ones do not

if (impar %2 != 0)
{
    System . out .printf ("%3d",impar); 
}
impar++;
    
28.10.2017 / 12:25
4

Remove this line:

System.out.println("   ");
    
28.10.2017 / 12:25