C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Print sum of first N natural numbers

Introduction

The sum of first N natural numbers can be calculated in two ways:

  1. Using a formula:

 Formula

  1. Using a loop to add numbers from 1 to N.

This program will use both methods to demonstrate.

 

C Program: Print sum of first N natural 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 a positive integer N: ");

    scanf("%d", &N);

 

    // Using for loop

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

        sum += i;  // sum = sum + i

    }

 

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

 

    // Using formula

    int sum_formula = N * (N + 1) / 2;

    printf("Sum of first %d natural numbers (using formula) = %d\n", N, sum_formula);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter a positive integer N: 10
Sum of first 10 natural numbers (using loop) = 55
Sum of first 10 natural numbers (using formula) = 55

OUTPUT 2 :
Enter a positive integer N: 5
Sum of first 5 natural numbers (using loop) = 15
Sum of first 5 natural numbers (using formula) = 15

Explanation

  1. User enters N.
  2. Loop method:
    • Start sum = 0.
    • Add each number from 1 to N.
  3. Formula method:
    • Directly calculate using N*(N+1)/2.
  4. Both results are displayed for verification.

 

C Program: Print sum of first N natural numbers

Method 2: Using while loop

The while loop can be used to iteratively add numbers from 1 to N.
This approach is useful for beginners to understand loops and accumulators.

 

C

#include <stdio.h>

 

int main() {

    int N, i = 1, sum = 0;

 

    // Input

    printf("Enter a positive integer N: ");

    scanf("%d", &N);

 

    // While loop to calculate sum

    while (i <= N) {

        sum += i;  // sum = sum + i

        i++;       // increment counter

    }

 

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

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter a positive integer N: 8
Sum of first 8 natural numbers = 36

OUTPUT 2 :
Enter a positive integer N: 5
Sum of first 5 natural numbers = 15

Explanation

  1. Initialize i = 1 and sum = 0.
  2. Loop continues while i <= N.
  3. Each iteration: add i to sum and increment i.
  4. Loop stops when i > N.
  5. Print the total sum.

 

C Program: Print sum of first N natural numbers

Method 3: Using do..while loop

A do-while loop executes the loop body at least once, making it suitable for calculating sums where N ≥ 1.
We’ll iteratively add numbers from 1 to N using this loop.

C

#include <stdio.h>

 

int main() {

    int N, i = 1, sum = 0;

 

    // Input

    printf("Enter a positive integer N: ");

    scanf("%d", &N);

 

    // Do-while loop to calculate sum

    do {

        sum += i;  // Add current number to sum

        i++;       // Increment counter

    } while (i <= N);

 

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

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter a positive integer N: 7
Sum of first 7 natural numbers = 28


OUTPUT 2 :
Enter a positive integer N: 5
Sum of first 5 natural numbers = 15


Explanation

  1. Initialize i = 1 and sum = 0.
  2. The do block executes first — adds i to sum.
  3. Increment i by 1.
  4. The loop continues while i <= N.
  5. Print the final sum after the loop ends.