0
0
CProgramBeginner · 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++) printf("%d\n", i); inside your main function.
📋

Examples

Input1
Output1
Input5
Output1 2 3 4 5
Input0
Output
🧠

How to Think About It

To print numbers from 1 to n, start counting from 1 and keep increasing the count by 1 until you reach n. For each number, print it on the screen. If n is zero or less, print nothing.
📐

Algorithm

1
Get 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

c
#include <stdio.h>

int main() {
    int n, i;
    scanf("%d", &n);
    for(i = 1; i <= n; i++) {
        printf("%d\n", i);
    }
    return 0;
}
Output
1 2 3 4 5
🔍

Dry Run

Let's trace the program with input n = 5 through the code

1

Input

User enters n = 5

2

Initialize counter

Set i = 1

3

Check condition

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

4

Print number

Print 1

5

Increment counter

i becomes 2

6

Repeat loop

Check if i (2) <= 5, print 2, increment i to 3, and so on until i = 6

7

End loop

When i = 6, condition i <= 5 is false, stop loop

iCondition i <= nAction
1truePrint 1, i++ to 2
2truePrint 2, i++ to 3
3truePrint 3, i++ to 4
4truePrint 4, i++ to 5
5truePrint 5, i++ to 6
6falseExit loop
💡

Why This Works

Step 1: Reading input

The program reads the number n from the user using scanf to know how many numbers to print.

Step 2: Looping from 1 to n

The for loop starts at 1 and runs until it reaches n, printing each number in order.

Step 3: Printing numbers

Inside the loop, printf outputs the current number followed by a new line, so each number appears on its own line.

🔄

Alternative Approaches

while loop
c
#include <stdio.h>

int main() {
    int n, i = 1;
    scanf("%d", &n);
    while(i <= n) {
        printf("%d\n", i);
        i++;
    }
    return 0;
}
Uses a while loop instead of for; functionally the same but some find it clearer for beginners.
do-while loop
c
#include <stdio.h>

int main() {
    int n, i = 1;
    scanf("%d", &n);
    if(n > 0) {
        do {
            printf("%d\n", i);
            i++;
        } while(i <= n);
    }
    return 0;
}
Uses do-while to ensure at least one print if n > 0; requires an if check to avoid printing when 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 number of variables and prints output directly, 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)When condition checked before each iteration
do-while loopO(n)O(1)When at least one iteration is needed
💡
Always check that n is positive before printing to avoid unexpected output.
⚠️
Forgetting to initialize the loop counter to 1 or using incorrect loop conditions.