C Program to Print Even Numbers from 1 to n
Use a
for loop from 1 to n and print numbers where i % 2 == 0, like: for(int i=1; i<=n; i++) { if(i % 2 == 0) printf("%d ", i); }.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 counting from 1 up to n. For each number, check if it divides evenly by 2 (no remainder). If yes, print it. This way, only even numbers appear.
Algorithm
1
Get input number n from the user2
Start a loop from 1 to n3
For each number, check if it is divisible by 24
If divisible, print the number5
End the loop after reaching nCode
c
#include <stdio.h> int main() { int n; printf("Enter the value of n: "); scanf("%d", &n); for (int i = 1; i <= n; i++) { if (i % 2 == 0) { printf("%d ", i); } } return 0; }
Output
Enter the value of n: 10
2 4 6 8 10
Dry Run
Let's trace input n=5 through the code
1
Input n
User enters n = 5
2
Loop from 1 to n
i starts at 1
3
Check if i is even
i=1, 1 % 2 = 1 (not even), skip print
4
Next i
i=2, 2 % 2 = 0 (even), print 2
5
Continue loop
i=3 (odd, skip), i=4 (even, print 4), i=5 (odd, skip)
6
End loop
Loop ends after i=5
| i | i % 2 == 0? | Printed |
|---|---|---|
| 1 | No | |
| 2 | Yes | 2 |
| 3 | No | |
| 4 | Yes | 4 |
| 5 | No |
Why This Works
Step 1: Loop through numbers
The for loop goes from 1 to n, checking each number.
Step 2: Check even condition
Using i % 2 == 0 tests if the number divides evenly by 2, meaning it is even.
Step 3: Print even numbers
Only numbers passing the even test are printed, so output shows even numbers from 1 to n.
Alternative Approaches
Start loop from 2 and increment by 2
c
#include <stdio.h> int main() { int n; printf("Enter the value of n: "); scanf("%d", &n); for (int i = 2; i <= n; i += 2) { printf("%d ", i); } return 0; }
This method skips checking odd numbers by starting at 2 and jumping by 2, making it more efficient.
Use while loop with increment
c
#include <stdio.h> int main() { int n, i = 2; printf("Enter the value of n: "); scanf("%d", &n); while (i <= n) { printf("%d ", i); i += 2; } return 0; }
Using a while loop with increments of 2 also prints even numbers efficiently without checking each number.
Complexity: O(n) time, O(1) space
Time Complexity
The loop runs from 1 to n, so it executes n times, making the time complexity O(n).
Space Complexity
The program uses a fixed amount of memory for variables, so space complexity is O(1).
Which Approach is Fastest?
Starting the loop at 2 and incrementing by 2 reduces the number of iterations by half, making it faster than checking each number.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Check each number with if | O(n) | O(1) | Simple and clear for beginners |
| Loop from 2, increment by 2 | O(n/2) | O(1) | More efficient, fewer iterations |
| While loop increment by 2 | O(n/2) | O(1) | Alternative loop style, efficient |
Start the loop at 2 and increase by 2 to print even numbers faster without extra checks.
Beginners often forget to check the condition
i % 2 == 0 and print all numbers instead of only even ones.