Java Program to Print Even Numbers from 1 to n
You can print even numbers from 1 to n in Java using a
for loop and checking if a number is divisible by 2 with if (i % 2 == 0), then printing it.Examples
Input5
Output2 4
Input10
Output2 4 6 8 10
Input1
Output
How to Think About It
To print even numbers from 1 to n, start from 1 and go up to n. For each number, check if it divides evenly by 2 using the remainder operator
%. If the remainder is zero, it means the number is even, so print it.Algorithm
1
Get input number n from the user2
Start a loop from 1 to n3
For each number i, check if i % 2 equals 04
If yes, print the number i5
Continue until the loop endsCode
java
import java.util.Scanner; public class EvenNumbers { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); for (int i = 1; i <= n; i++) { if (i % 2 == 0) { System.out.print(i + " "); } } scanner.close(); } }
Output
2 4 6 8 10
Dry Run
Let's trace input n=5 through the code
1
Input
User enters n = 5
2
Loop start
i = 1, check if 1 % 2 == 0 (false), do not print
3
Loop continues
i = 2, check if 2 % 2 == 0 (true), print 2
4
Next iterations
i = 3 (false), i = 4 (true, print 4), i = 5 (false)
5
Loop ends
Printed numbers: 2 4
| i | i % 2 == 0? | Printed |
|---|---|---|
| 1 | false | |
| 2 | true | 2 |
| 3 | false | |
| 4 | true | 4 |
| 5 | false |
Why This Works
Step 1: Loop through numbers
The for loop goes from 1 to n, checking each number one by one.
Step 2: Check even condition
Using i % 2 == 0 checks if the number divides evenly by 2, meaning it is even.
Step 3: Print even numbers
If the number is even, it is printed followed by a space to separate numbers.
Alternative Approaches
Start loop from 2 and increment by 2
java
import java.util.Scanner; public class EvenNumbers { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); for (int i = 2; i <= n; i += 2) { System.out.print(i + " "); } scanner.close(); } }
This method skips checking odd numbers by starting at 2 and jumping by 2, making it more efficient.
Use while loop with condition
java
import java.util.Scanner; public class EvenNumbers { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int i = 2; while (i <= n) { System.out.print(i + " "); i += 2; } scanner.close(); } }
Using a while loop with increments of 2 also prints even numbers efficiently.
Complexity: O(n) time, O(1) space
Time Complexity
The program loops from 1 to n once, so it runs in linear time O(n).
Space Complexity
It uses only a few variables and prints output directly, so space is constant O(1).
Which Approach is Fastest?
Starting the loop at 2 and incrementing by 2 is faster because it skips odd numbers and reduces the number of iterations by half.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Check each number with if | O(n) | O(1) | Simple and clear logic |
| Loop from 2, increment by 2 | O(n/2) | O(1) | More efficient, fewer checks |
| While loop increment by 2 | O(n/2) | O(1) | Alternative loop style, efficient |
Start your loop at 2 and increase by 2 to print even numbers without extra checks.
Beginners often forget to check the remainder with
% or start from 1 instead of 2, printing odd numbers by mistake.