In this tutorial, we will learn how to write a Java program to print even numbers from 1 to N, taking N as input from the user.
For example, if the user enters 20, the program will print all even numbers from 1 to 20. We can achieve this using different looping statements in Java, such as for, while, and do-while.
Before diving into each approach, let’s first see how to identify even numbers in Java.
How to Check Whether a Number Is Even in Java?
An even number is an integer that is exactly divisible by 2, leaving a remainder of 0. For example, 2, 4, 6, 8, 10, and 12 are even numbers. In Java, we use the remainder (modulus) operator (%) on integer types (int or long) to get the remainder after division.
Condition
The condition to check whether a number is an even is:
number % 2 == 0
For example:
8 % 2 == 0 evaluates to true because 8 divided by 2 leaves a remainder of 0.
7 % 2 == 0 evaluates to false because 7 % 2 evaluates to 1.
Therefore, if the remainder of a number divided by 2 is 0, it is an even number.
Java Program to Print Even Numbers from 1 to N Using for Loop
The for loop is one of the simplest approaches to print even numbers from 1 to N in Java. In this approach, we initialize a loop variable i with 1 and execute the loop until its value becomes greater than N. Inside the loop, we use an if statement to check whether the current number is divisible by 2.
If the condition i % 2 == 0 is true, the program prints the number. Otherwise, it skips that number.
Let’s write a complete Java program to print even numbers from 1 to N using for loop.
Complete Java Program
import java.util.Scanner;
public class PrintEvenNumbers {
public static void main(String[] args) {
// Create a Scanner object to read input from the console
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the value of N
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
// Ensure N is at least 1 so there is a valid range from 1 to N
if (n >= 1) {
System.out.println("Even numbers from 1 to " + n + " are:");
// Loop from 1 up to n (inclusive)
for (int i = 1; i <= n; i++) {
// Check if the current number is divisible by 2
if (i % 2 == 0) {
System.out.print(i + " ");
}
}
System.out.println(); // Move to a new line after printing all even numbers
} else {
// Handle zero or negative values of N
System.out.println("Please enter a positive integer.");
}
// Close the Scanner to release system resources
scanner.close();
}
}Output:
Enter the value of N: 20 Even numbers from 1 to 20 are: 2 4 6 8 10 12 14 16 18 20
Explanation of the Java Program
Let’s understand how this Java program runs step by step.
- Import the Scanner class: First, we import the Scanner class from the java.util package to accept input from the keyboard.
- Create a Scanner object: We create an instance of the Scanner class to read the user’s input.
- Read the value of N: We invoke the nextInt() method, which reads an integer entered by the user and stores it in the variable n.
- Validate the input: We use an if statement to check whether the entered number is greater than or equal to 1. If the value of n is less than 1, it prints a prompt asking for a positive integer instead of running the loop.
- Initialize the loop counter variable: Inside the for loop, we initialize the variable i with 1.
- Check the loop condition: The condition
i <= nchecks whether the current value of i is less than or equal to N. If the condition is true, the code inside the loop body executes. - Check for an even number: The condition
i % 2 == 0checks whether the current number is exactly divisible by 2. If true, it is an even number. - Print the even number: When the condition
i % 2 == 0is true, the statementSystem.out.print(i + " ")prints the current even number (i.e., the value of i) followed by a space. - Increment the variable: The increment operator (
i++) increases the value of i by 1 at the end of each iteration to move to the next integer. - Terminate the loop: Once the value of i becomes greater than n, the condition
i <= nis false and the loop ends.
As a result, the program prints every even number from 1 up to n.
Java Program to Print Even Numbers Without Using If Statement
We can also print even numbers in Java from 1 to N without using an if statement. This approach is more efficient because we do not need to check every number to determine whether it is even or odd.
Since 2 is the first positive even number, we initialize the loop variable i with 2 and increase its value by 2 after every iteration.
Let’s write a program to print even numbers in Java without using an if statement.
Complete Java Program
import java.util.Scanner;
public class PrintEvenNumbersWithoutIf {
public static void main(String[] args) {
// Create a Scanner object to read input from the console
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the value of N
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
// Check whether N is at least 1 so there is a valid range from 1 to N
if (n >= 1) {
System.out.println("Even numbers from 1 to " + n + " are:");
// Start at 2 (the first even number) and increment by 2 each step
for (int i = 2; i <= n; i += 2) {
System.out.print(i + " ");
}
System.out.println(); // Move to a new line after printing all even numbers
} else {
// Handle zero or negative input
System.out.println("Please enter a positive integer.");
}
// Close the Scanner to release system resources
scanner.close();
}
}Output:
Enter the value of N: 25 Even numbers from 1 to 25 are: 2 4 6 8 10 12 14 16 18 20 22 24
Explanation of the Java Program
Let’s understand the important statements used in this program.
- Initialize the loop variable: We initialize the counter variable i with 2 because 2 is the first positive even number.
- Check the loop condition: The condition
i <= nensures that the loop executes only while the current value of i is less than or equal to N. - Print the even number: The
System.out.print(i + " ")statement prints the current even number followed by a space. - Increase the value by 2: The expression
i += 2increases the value of i by 2 after each iteration. It is equivalent toi = i + 2.
Since we increase the value by 2 each time, the loop directly generates even numbers without checking odd numbers.
Note: This approach is preferable when you only need to print even numbers because it avoids unnecessary iterations and the modulus operation.
Print Even Numbers in Java Using While Loop
We can also print all even numbers from 1 to N using a while loop in Java. A while loop repeatedly executes a block of statements as long as its specified condition evaluates to true.
In this approach, we initialize a control variable i with 2 before the loop starts. The while loop checks whether i is less than or equal to N. If the condition evaluates to true, it prints the current number and increases its value by 2.
Let’s look at the complete Java program to print even numbers using while loop.
Complete Java Program
import java.util.Scanner;
public class PrintEvenNumbersWhile {
public static void main(String[] args) {
// Create a Scanner object to read input from the console
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the upper limit N
System.out.print("Enter the value of N: ");
int n = scanner.nextInt();
// Check whether N is at least 1 so there is a valid range from 1 to N
if (n >= 1) {
System.out.println("Even numbers from 1 to " + n + " are:");
// Initialize loop variable to the first even number
int i = 2;
// Repeat while i is within the range 1 to n
while (i <= n) {
// Print the current even number followed by a space
System.out.print(i + " ");
// Advance to the next even number
i += 2;
}
System.out.println(); // Move to a new line after printing all even numbers
} else {
// Handle zero or negative values of N
System.out.println("Please enter a positive integer.");
}
// Close the Scanner to release system resources
scanner.close();
}
}Output:
Enter the value of N: 16 Even numbers from 1 to 16 are: 2 4 6 8 10 12 14 16
Coding Challenge: Print Even Numbers Using Do-While Loop in Java
Your Task:
Write a Java program to print all even numbers from 1 to N using a do-while loop.
Follow these instructions:
- Ask the user to enter an integer N.
- Initialize a variable i with 2 because 2 is the first positive even number.
- Use a do-while loop to print even numbers from 1 to N.
- Increase the value of i by 2 after each iteration.
- Since a do-while loop executes at least once, make sure the loop executes only when N is greater than or equal to 2.
- Display an appropriate message if the entered number is less than 1.
Hint: Initialize i = 2, print its value inside the do block, and use i += 2 to generate the next even number. Check the condition i <= n in the while statement.
Additional Practice: What should your program display if the user enters N = 1 or N = 15? Try these test cases to verify your program.


