Java Loops Exercise 1

Java code examples for printing even and odd numbers within specific ranges using loops and conditional statements

The following Java code demonstrates how to print even numbers from 0 to 10 using the remainder of division and then prints odd numbers from 100 to 150, followed by even numbers from 150 to 200.

package esercizio_cicli;
public class Esercizio_Cicli {
public static void main(String[] args) {
// print even numbers from 0 to 10 using the remainder of division
int x = 2;
int y = 0;
while(y <= 100) {
if(y % x == 0) {
System.out.println(y);
}
y++;
}

/* print odd numbers from 100 to 150
* and then print even numbers from 150
* to 200
*/
int p = 2;
int z = 99;
System.out.println("now I say the odd numbers from 100 to 150");
boolean bool = true;
while(bool) {

z++;
if(z % p != 0) {

System.out.println(z);

}
if(z == 150) {

bool = false;
z = 149;
System.out.println("Now I say the even numbers from 150 to 200");

}

}
while(!bool) {
z++;
if(z % p == 0) {

System.out.println(z);

}

if(z == 200) {

bool = true;

}

}

}
}