C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Print first N odd numbers

Introduction

Odd numbers are integers that are not divisible by 2 — examples include 1, 3, 5, 7, 9, etc.
The general formula for the i-th odd number is:

Odd Number = 2 × i − 1

This program prints the first N odd numbers using a for loop.

 

C Program: Print first N odd numbers

NOTE: The Program is written using for, while and do..while loop.
Method 1 is for loop, Method 2 is while loop and Method 3 is do..while loop

Method 1: Using for loop

C

#include <stdio.h>

 

int main() {

    int N, i;

 

    // Input

    printf("Enter the value of N: ");

    scanf("%d", &N);

 

    printf("First %d odd numbers are:\n", N);

 

    // Loop to print odd numbers

    for (i = 1; i <= N; i++) {

        printf("%d ", 2 * i - 1);

    }

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 10
First 10 odd numbers are:
1 3 5 7 9 11 13 15 17 19

OUTPUT 2 :
Enter the value of N: 5
First 5 odd numbers are:
1 3 5 7 9

Explanation

  1. The user enters N.
  2. The for loop runs from 1 to N.
  3. Each time, the formula 2 * i - 1 calculates the next odd number.
  4. The program prints all odd numbers from 1 up to the Nth odd number.

 

C Program: Print first N odd numbers

Method 2: Using while loop

Odd numbers are numbers that are not divisible by 2, such as 1, 3, 5, 7, etc.
In this program, we’ll use a while loop to print the first N odd numbers, where each odd number can be calculated using the formula:

Odd Number = 2 × i − 1

C

#include <stdio.h>

 

int main() {

    int N, i = 1;

 

    // Input

    printf("Enter the value of N: ");

    scanf("%d", &N);

 

    printf("First %d odd numbers are:\n", N);

 

    // While loop

    while (i <= N) {

        printf("%d ", 2 * i - 1);

        i++;  // increment counter

    }

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 7
First 7 odd numbers are:
1 3 5 7 9 11 13

OUTPUT 2 :
Enter the value of N: 5
First 5 odd numbers are:
1 3 5 7 9

Explanation

  1. The user enters N, the number of odd numbers to print.
  2. The counter i starts from 1.
  3. The while loop runs while i <= N.
  4. In each iteration, it prints 2 * i - 1 — the i-th odd number.
  5. i is incremented by 1 in each loop.

 

C Program: Print first N odd numbers

Method 3: Using do..while loop

The do-while loop ensures that the loop runs at least once before checking the condition.
We’ll use the formula:

Odd Number = 2 × i – 1

to print the first N odd numbers.

 

C

#include <stdio.h>

 

int main() {

    int N, i = 1;

 

    // Input

    printf("Enter the value of N: ");

    scanf("%d", &N);

 

    printf("First %d odd numbers are:\n", N);

 

    // Do-while loop

    do {

        printf("%d ", 2 * i - 1);

        i++;

    } while (i <= N);

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 6
First 6 odd numbers are:
1 3 5 7 9 11

OUTPUT 2 :
Enter the value of N: 4
First 4 odd numbers are:
1 3 5 7

Explanation

  1. i starts from 1.
  2. The do block executes first — printing 2 * i - 1.
  3. i is incremented by 1.
  4. The condition i <= N is checked after each iteration.
  5. The loop continues until i becomes greater than N.