0
0
CProgramBeginner · 2 min read

C Program to Print Odd Numbers from 1 to n

Use a for loop starting from 1 to n incrementing by 2 and print each number with printf, like for(int i=1; i<=n; i+=2) printf("%d\n", i);.
📋

Examples

Input5
Output1 3 5
Input10
Output1 3 5 7 9
Input1
Output1
🧠

How to Think About It

To print odd numbers from 1 to n, start counting from 1 and keep adding 2 each time to get the next odd number. Stop when you reach or pass n. This way, you only print odd numbers without checking each number.
📐

Algorithm

1
Get input number n from the user
2
Start a loop with a variable i from 1 to n
3
Increase i by 2 in each step to get only odd numbers
4
Print the current value of i in each loop iteration
5
Stop the loop when i becomes greater than n
💻

Code

c
#include <stdio.h>

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

Dry Run

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

1

Input

User enters n = 5

2

Initialize loop variable

i = 1

3

Check condition

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

4

Print i

Print 1

5

Increment i

i = i + 2 = 3

6

Repeat condition check

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

7

Print i

Print 3

8

Increment i

i = i + 2 = 5

9

Repeat condition check

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

10

Print i

Print 5

11

Increment i

i = i + 2 = 7

12

Repeat condition check

Is i (7) <= n (5)? No, stop loop

i
1
3
5
💡

Why This Works

Step 1: Start from 1

We start the loop at 1 because 1 is the first odd number.

Step 2: Increment by 2

Adding 2 each time ensures we only get odd numbers (1, 3, 5, ...).

Step 3: Stop at n

The loop stops when the number exceeds n, so we print only odd numbers up to n.

🔄

Alternative Approaches

Check each number if odd
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\n", i);
        }
    }
    return 0;
}
This method checks every number from 1 to n, which is simpler but less efficient than incrementing by 2.
Use while loop
c
#include <stdio.h>

int main() {
    int n, i = 1;
    printf("Enter the value of n: ");
    scanf("%d", &n);
    while (i <= n) {
        printf("%d\n", i);
        i += 2;
    }
    return 0;
}
Using a while loop works the same way but some may find it easier to read.

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, so space complexity is O(1).

Which Approach is Fastest?

Incrementing by 2 is faster than checking each number with a condition because it skips even numbers entirely.

ApproachTimeSpaceBest For
Increment by 2O(n)O(1)Efficient and simple for printing odd numbers
Check each numberO(n)O(1)Simple logic but less efficient
While loop increment by 2O(n)O(1)Alternative loop style, same efficiency
💡
Increment the loop variable by 2 to directly get odd numbers without extra checks.
⚠️
Beginners often forget to increment by 2 and print even numbers or all numbers instead.