Java Program to Display Even Numbers
To display even numbers in Java, you can either start at 2 and increase the loop variable by 2, or visit every integer and print only the values for which number % 2 == 0. This tutorial shows both approaches with while and for loops.
The examples below print positive even numbers from 1 up to a given maximum n. For example, when n = 20, the result is 2, 4, 6, ..., 20.
How Java Checks Whether a Number Is Even
An integer is even when it is exactly divisible by 2. In Java, the remainder operator % gives the remainder after division. Therefore, an integer is even when dividing it by 2 leaves a remainder of 0.
number % 2 == 0
For example, 8 % 2 is 0, so 8 is even. In contrast, 7 % 2 is 1, so 7 is not even.
If the required range begins at 1, then 2 is the first positive even number. Although 0 is also an even integer, it is outside the 1-to-n range used in these examples.
Algorithms to Print Even Numbers up to N in Java
Algorithm 1 – Start at 2 and Increment by 2
This approach visits only even numbers. Start with even = 2, print it while it is less than or equal to n, and add 2 after every iteration.
- Start.
- Take a value for n. This is the upper limit for the even numbers printed to the console.
- Initialize variable even with
2. - Check whether even is less than or equal to n.
- If the condition is true, continue to step 6; otherwise, go to step 9.
- Print even.
- Increment even by
2. - Go to step 4.
- Stop.
Because this method advances directly from one even number to the next, it performs fewer loop iterations than checking every integer in the range.
Algorithm 2 – Check Each Number with the Remainder Operator
This alternative loops through every integer from 1 to n and checks whether the current value is even using i % 2 == 0.
- Start.
- Take a value for n. This is the upper limit for the even numbers printed to the console.
- Initialize variable i with
1. - Check whether i is less than or equal to n.
- If the condition is true, continue to step 6; otherwise, go to step 11.
- Check whether i is exactly divisible by
2. - If the condition is true, continue to step 8; otherwise, go to step 9.
- Print i.
- Increment i by
1. - Go to step 4.
- Stop.
Both algorithms produce the same output. The first is a direct way to generate even numbers, while the second is useful when you specifically want to practice checking whether each value is even.
Display Even Numbers up to N Using a Java 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;
}
}
}
Here, even starts at 2. After each print operation, even += 2 moves directly to the next even number. The loop stops when even becomes greater than n.
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++;
}
}
}
In the second program, the loop visits every integer from 1 through 20. The if statement prints only values whose remainder after division by 2 is zero.
Output
Run either of the above programs, and all even numbers up to n are printed to the console.
2 4 6 8 10 12 14 16 18 20
Display Even Numbers up to N Using a Java 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+" ");
}
}
}
The for loop places initialization, the upper-limit condition, and the increment in one statement: start at 2, continue while i <= n, and increase i by 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
for (int i=1; i<=n; i++) {
if(i%2==0) {
System.out.print(i+" ");
}
}
}
}
This version checks every value from 1 to n. Use this form when the condition that identifies an even number is part of what you want to demonstrate.
Output
Run either of the above programs, and all even numbers up to n are printed to the console.
2 4 6 8 10 12 14 16 18 20
Print Even Numbers from 1 to a User-Entered N in Java
If the upper limit should not be fixed in the source code, read n from the user and then run the same loop. The following example uses Scanner and prints all positive even numbers from 1 through the entered value.
import java.util.Scanner;
public class DisplayEvenNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter N: ");
int n = scanner.nextInt();
for (int i = 2; i <= n; i += 2) {
System.out.print(i + " ");
}
scanner.close();
}
}
For an input of 20, the program prints the same sequence as the earlier examples.
Enter N: 20
2 4 6 8 10 12 14 16 18 20
If n is less than 2, this loop prints no positive even numbers because its starting value is 2.
Print the First N Even Numbers in Java
Printing even numbers up to a maximum value is different from printing the first N even numbers. If you need the first n positive even numbers, let the loop count from 1 through n and print 2 * i.
public class FirstNEvenNumbers {
public static void main(String[] args) {
int n = 10;
for (int i = 1; i <= n; i++) {
System.out.print((2 * i) + " ");
}
}
}
For n = 10, the program prints the first 10 positive even numbers.
2 4 6 8 10 12 14 16 18 20
Choosing Between the Two Java Even-Number Loop Patterns
- Use
i += 2when you only need to generate even numbers. It visits exactly the values you want to print. - Use
i % 2 == 0when you are iterating through every integer and need to test whether each value is even. - Use
2 * iwhen the requirement is to print the first n positive even numbers rather than all even numbers up to an upper limit.
Java Even Number Printing: Key Points
In this Java Tutorial, we learned how to print positive even numbers up to a given number using while and for loops. We also used the remainder condition number % 2 == 0, accepted the upper limit from user input, and distinguished an upper-limit problem from printing the first n even numbers.
TutorialKart.com