Java – Display Even Numbers

In this tutorial, we shall write Java Programs that print all even numbers from starting up to the given maximum.

You can use looping techniques, to iterate for each even number until a threshold, or maximum. We shall use for loop and while loop to iterate over the even numbers up to we reach the end.

Or alternatively, you can iterate from 1 to n, in steps of 1, and check if the number is even during each iteration.

Let us write algorithms for these two approaches.

Algorithms to Print Even Numbers up to N

Algorithm 1 – Increment in steps of 2

Following is an algorithm using which we can print even numbers til by incrementing the loop variable with 2 during each iteration.

  1. Start.
  2. Take a value for n. This is our upper limit for the even numbers we print to console.
  3. Initialize variable even with 2. Even numbers start from 2.
  4. Check if even is less than or equal to n.
  5. If the condition is true, go to step 6, else go to step 9.
  6. Print even.
  7. Increment even by 2. So, that even has the next even number in the next iteration.
  8. Go to step 4.
  9. Stop.

Algorithm 2 – Check if Number is Even

Following is an alternative way to print even numbers up to by checking during each iteration if the number is odd.

  1. Start.
  2. Take a value for n. This is our upper limit for the even numbers we print to console.
  3. Initialize variable i with 1.
  4. Check if i is less than or equal to n.
  5. If the above condition is true, go to step 6, else go to step 11.
  6. Check if i is exactly divisible by 2.
  7. If the above condition is true, go to step 8, else go to step 9.
  8. Print i.
  9. Increment i by 1.
  10. Go to step 4.
  11. Stop.

We shall go through each of these algorithms in our examples, using different looping mechanisms.

ADVERTISEMENT

Example 1 – Display Even Numbers upto N – While Loop

In this example, we shall print all the even numbers less than or equal to 20 using Java While Loop. We shall write the Java program based on the above algorithms.

Java Program – Using Algorithm 1

/**
 * Java Program - Display Even Numbers
 */

public class DisplayEvenNumbers {

	public static void main(String[] args) {
		//number
		int n = 20;
		
		//print all even numbers <=n 
		int even=2;
		while (even<=n) {
			System.out.print(even+"  ");
			even += 2;
		}
	}
}

Java Program – Using Algorithm 2

/**
 * Java Program - Display Even Numbers
 */

public class DisplayEvenNumbers {

	public static void main(String[] args) {
		//number
		int n = 20;
		
		//print all even numbers <=n 
		int i=1;
		while (i<=n) {
			//check if i is exactly divisible by 2
			if (i%2==0) {
				System.out.print(i+"  ");
			}
			i++;
		}
	}
}

Output

Run any of the above program, and we shall get all the even numbers up to n, printed to the console.

2  4  6  8  10  12  14  16  18  20

Example 2 – Display Even Numbers upto N – For Loop

In this example, we shall print all the even numbers less than or equal to 20 using Java For Loop. We shall write the Java program based on the algorithms we already laid out.

Java Program – Using Algorithm 1

/**
 * Java Program - Display Even Numbers
 */

public class DisplayEvenNumbers {

	public static void main(String[] args) {
		//number
		int n = 20;
		
		//print all even numbers <=n 
		for (int i=2; i<=n; i+=2) {
			System.out.print(i+"  ");
		}
	}
}

Java Program – Using Algorithm 2

/**
 * Java Program - Display Even Numbers
 */

public class DisplayEvenNumbers {

	public static void main(String[] args) {
		//number
		int n = 20;
		
		//print all even numbers <=n 
		for (int i=1; i<=n; i++) {
			if(i%2==0) {
				System.out.print(i+"  ");
			}
		}
	}
}

Output

Run the above program, and we shall get all the even numbers up to n, printed to the console.

2  4  6  8  10  12  14  16  18  20

Conclusion

In this Java Tutorial, we learned how to print all the even numbers up to a given number.