0
0
CppProgramBeginner · 2 min read

C++ Program to Print Numbers 1 to n

You can print numbers from 1 to n in C++ using a for loop like this: for (int i = 1; i <= n; ++i) std::cout << i << std::endl;.
📋

Examples

Input3
Output1 2 3
Input1
Output1
Input0
Output
🧠

How to Think About It

To print numbers from 1 to n, start counting from 1 and keep increasing the number by 1 until you reach n. For each number, print it on the screen. Stop when you have printed n.
📐

Algorithm

1
Get the input number n from the user.
2
Start a counter i at 1.
3
While i is less than or equal to n, do the following:
4
Print the current value of i.
5
Increase i by 1.
6
Stop when i becomes greater than n.
💻

Code

cpp
#include <iostream>

int main() {
    int n;
    std::cin >> n;
    for (int i = 1; i <= n; ++i) {
        std::cout << i << std::endl;
    }
    return 0;
}
Output
1 2 3
🔍

Dry Run

Let's trace input n=3 through the code

1

Input

User enters n = 3

2

Initialize counter

Set i = 1

3

Check condition

Is i (1) <= n (3)? Yes, continue

4

Print number

Print 1

5

Increment counter

i becomes 2

6

Repeat condition

Is i (2) <= n (3)? Yes, continue

7

Print number

Print 2

8

Increment counter

i becomes 3

9

Repeat condition

Is i (3) <= n (3)? Yes, continue

10

Print number

Print 3

11

Increment counter

i becomes 4

12

Check condition

Is i (4) <= n (3)? No, stop loop

iPrinted Number
11
22
33
💡

Why This Works

Step 1: Input reading

The program reads the number n which tells how many numbers to print.

Step 2: Loop from 1 to n

The for loop starts at 1 and runs until it reaches n, increasing by 1 each time.

Step 3: Print each number

Inside the loop, the current number i is printed on its own line.

🔄

Alternative Approaches

while loop
cpp
#include <iostream>

int main() {
    int n;
    std::cin >> n;
    int i = 1;
    while (i <= n) {
        std::cout << i << std::endl;
        ++i;
    }
    return 0;
}
Uses a while loop instead of for; functionally the same but shows a different looping style.
do-while loop
cpp
#include <iostream>

int main() {
    int n;
    std::cin >> n;
    int i = 1;
    if (n > 0) {
        do {
            std::cout << i << std::endl;
            ++i;
        } while (i <= n);
    }
    return 0;
}
Uses do-while loop to ensure at least one print if n > 0; requires extra check for n > 0.

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?

All loop methods (for, while, do-while) have the same time and space complexity; choice depends on readability and style.

ApproachTimeSpaceBest For
for loopO(n)O(1)Simple counting with known range
while loopO(n)O(1)Flexible looping with condition
do-while loopO(n)O(1)At least one iteration guaranteed
💡
Always check that n is positive before printing to avoid unexpected output.
⚠️
Forgetting to increment the counter inside the loop causes an infinite loop.