0
0
CppProgramBeginner · 2 min read

C++ Program to Print Even Numbers

A C++ program to print even numbers uses a for loop starting from 2 and increments by 2, like for(int i = 2; i <= n; i += 2) std::cout << i << ' '; to print even numbers up to n.
📋

Examples

Input10
Output2 4 6 8 10
Input5
Output2 4
Input1
Output
🧠

How to Think About It

To print even numbers up to a given number, start from 2 (the first even number) and keep adding 2 each time until you reach or pass the given number. This way, you only print even numbers without checking each number.
📐

Algorithm

1
Get the maximum number n from the user.
2
Start a loop from 2 to n, increasing by 2 each time.
3
Print the current number in the loop.
4
Stop when the current number exceeds n.
💻

Code

cpp
#include <iostream>

int main() {
    int n;
    std::cout << "Enter the maximum number: ";
    std::cin >> n;
    for (int i = 2; i <= n; i += 2) {
        std::cout << i << " ";
    }
    std::cout << std::endl;
    return 0;
}
Output
Enter the maximum number: 10 2 4 6 8 10
🔍

Dry Run

Let's trace input 10 through the code

1

Input

User enters n = 10

2

Loop start

i = 2, check if i <= 10 (true), print 2

3

Loop continues

i = 4, check if i <= 10 (true), print 4

4

Loop continues

i = 6, check if i <= 10 (true), print 6

5

Loop continues

i = 8, check if i <= 10 (true), print 8

6

Loop continues

i = 10, check if i <= 10 (true), print 10

7

Loop ends

i = 12, check if i <= 10 (false), stop loop

iCondition i <= nAction
2trueprint 2
4trueprint 4
6trueprint 6
8trueprint 8
10trueprint 10
12falsestop
💡

Why This Works

Step 1: Start from 2

We start from 2 because it is the smallest even number and the first number we want to print.

Step 2: Increment by 2

Increasing the loop variable by 2 each time ensures we only get even numbers without extra checks.

Step 3: Loop condition

The loop runs while the current number is less than or equal to the input number, so we don't print numbers beyond the limit.

🔄

Alternative Approaches

Check each number if even
cpp
#include <iostream>

int main() {
    int n;
    std::cout << "Enter the maximum number: ";
    std::cin >> n;
    for (int i = 1; i <= n; i++) {
        if (i % 2 == 0) {
            std::cout << i << " ";
        }
    }
    std::cout << std::endl;
    return 0;
}
This method checks every number if it is even, which is less efficient but easy to understand.
Using while loop
cpp
#include <iostream>

int main() {
    int n, i = 2;
    std::cout << "Enter the maximum number: ";
    std::cin >> n;
    while (i <= n) {
        std::cout << i << " ";
        i += 2;
    }
    std::cout << std::endl;
    return 0;
}
Using a while loop instead of for loop achieves the same result with slightly different syntax.

Complexity: O(n/2) time, O(1) space

Time Complexity

The loop runs roughly n/2 times because it increments by 2, so time complexity is O(n/2), which simplifies to O(n).

Space Complexity

The program uses a fixed amount of memory for variables and does not use extra space proportional to input size, so space complexity is O(1).

Which Approach is Fastest?

Incrementing by 2 is faster than checking each number for evenness because it skips odd numbers entirely.

ApproachTimeSpaceBest For
Increment by 2 in for loopO(n)O(1)Efficient and simple
Check each number if evenO(n)O(1)Easy to understand but less efficient
While loop increment by 2O(n)O(1)Alternative syntax, same efficiency
💡
Increment the loop counter by 2 to print only even numbers efficiently.
⚠️
Beginners often forget to increment by 2 and print all numbers instead of just even ones.