Javatpoint Logo
Javatpoint Logo

Java Program to Display Even Numbers From 1 to 100

In this section, we will create a Java program to display even numbers from 1 to 100. To learn the Java even number program, you must have the basic knowledge of Java for loop and if statement.

We can use different ways to display even numbers:

  • Using Java for Loop
  • Using nested-if Statement
  • Using while Loop

Using Java for Loop

In the following example, we have declared a variable named number and initialized it with 100 (the limit to print the even number). We have used a for loop that executes up to 100 times and for each iteration of i the if statement checks the number is even or not. After printing each even number, the value if i is increased by 1.

In order to check the number, we have divided the number by 2 if it does not leave any remainder, the number is even and the print statement prints that number.

DisplayEvenNumbersExample1.java

Output:

List of even numbers from 1 to 100: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

Using nested-if Statement

The following program is slightly different from the above program because we have defined a method that contains the logic to check even number. Inside the method, we have used nested-if statement.

DisplayEvenNumbersExample2.java

Output:

List of even numbers: 
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

Using while Loop

In the following program, we have replaced the for loop with while loop.

DisplayEvenNumbersExample3.java

Output:

Enter the limit: 100
Lit of even numbers: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

Next TopicJava Programs





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA