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