# 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

1. public class DisplayEvenNumbersExample1
2. {
3. public static void main(String args[])
4. {
5. int number=100;
6. System.out.print(“List of even numbers from 1 to “+number+”: “);
7. for (int i=1; i<=number; i++)
8. {
9. //logic to check if the number is even or not
10. //if i%2 is equal to zero, the number is even
11. if (i%2==0)
12. {
13. System.out.print(i + ” “);
14. }
15. }
16. }
17. }

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

1. public class DisplayEvennumbersExample2
2. {
3. public static void main(String[] args)
4. {
5. System.out.println(“List of even numbers: “);
6. //method calling
7. displayEvenNumbers(1, 100);
8. }
9. //method that checks the number is even or not
10. private static void displayEvenNumbers(int number, int end)
11. {
12. if(number>end)
13. return;
14. if(number%2==0)
15. {
16. //prints the even numbers
17. System.out.print(number +” “);
18. //calling the method and increments the number by 2 if the number is even
19. displayEvenNumbers(number + 2, end);
20. }
21. else
22. {
23. //increments the number by 1 if the number is odd
24. displayEvenNumbers(number + 1, end);
25. }
26. }
27. }

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

1. import java.util.Scanner;
2. public class DisplayEvenNumbersExample3
3. {
4. public static void main(String[] args)
5. {
6. int number, i;
7. Scanner sc=new Scanner(System.in);
8. System.out.print(“Enter the limit: “);
9. number = sc.nextInt();
10. i=2;
11. System.out.print(“Lit of even numbers: “);
12. //the while loop executes until the condition become false
13. while(i<=number)
14. {
15. //prints the even number
16. System.out.print(i +” “);
17. //increments the variable i by 2
18. i=i+2;
19. }
20. }
21. }

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```