C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Print sum of first N odd numbers

Introduction

Odd numbers are numbers that are not divisible by 2, such as 1, 3, 5, 7, 9, etc.
The formula for the sum of first N odd numbers is:

Sum=N2

However, we’ll calculate it using a loop, to understand how summation works step by step.

 

C Program: Print sum of 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, sum = 0;

 

    // Input

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

    scanf("%d", &N);

 

    // Loop to calculate sum of first N odd numbers

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

        sum += 2 * i - 1;  // ith odd number = 2*i - 1

    }

 

    printf("Sum of first %d odd numbers = %d\n", N, sum);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 5
Sum of first 5 odd numbers = 25

OUTPUT 2 :
Enter the value of N: 7
Sum of first 7 odd numbers = 49

Explanation

  1. The user inputs the value of N.
  2. The for loop runs from 1 to N.
  3. Each iteration adds the i-th odd number (2*i - 1) to sum.
  4. After the loop ends, the program prints the total sum.

 

C Program: Print sum of first N odd numbers

Method 2: Using while loop


C

#include <stdio.h>

 

int main() {

    int N, i = 1, sum = 0;

 

    // Input

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

    scanf("%d", &N);

 

    // While loop to calculate sum

    while (i <= N) {

        sum += 2 * i - 1;  // Add ith odd number

        i++;               // Increment counter

    }

 

    printf("Sum of first %d odd numbers = %d\n", N, sum);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 5
Sum of first 5 odd numbers = 25

OUTPUT 2 :
Enter the value of N: 8
Sum of first 8 odd numbers = 64

Explanation

  1. Start with i = 1 and sum = 0.
  2. The while loop executes while i <= N.
  3. In each iteration, 2 * i - 1 gives the current odd number.
  4. Add it to sum and increment i.
  5. Print the total sum after the loop ends.

 

C Program: Print sum of first N odd numbers

Method 3: Using do..while loop


C

#include <stdio.h>

 

int main() {

    int N, i = 1, sum = 0;

 

    // Input

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

    scanf("%d", &N);

 

    // Do-while loop to calculate sum

    do {

        sum += 2 * i - 1;  // Add the ith odd number

        i++;               // Increment counter

    } while (i <= N);

 

    printf("Sum of first %d odd numbers = %d\n", N, sum);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 5
Sum of first 5 odd numbers = 25

OUTPUT 2 :
Enter the value of N: 7
Sum of first 7 odd numbers = 49


Explanation

  1. The user enters the number N.
  2. The loop starts with i = 1.
  3. Inside the loop:
    • Calculates the odd number using 2 * i - 1.
    • Adds it to sum.
    • Increments i.
  4. The condition (i <= N) is checked after each iteration.
  5. Prints the final sum when the loop ends.