C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Print sum of first N even numbers

Introduction

Even numbers are multiples of 2 (2, 4, 6, …).
The sum of the first N even numbers can be calculated using two methods:

  1. Formula:

 C Programs

  1. Loop: Add numbers from 2 to the N-th even number using iteration.

We’ll use a loop method in this program.

 

C Program: Print sum of first N even 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 even numbers

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

        sum += 2 * i;  // ith even number = 2 * i

    }

 

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

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 5
Sum of first 5 even numbers = 30

OUTPUT 2 :
Enter the value of N: 10
Sum of first 10 even numbers = 110

Explanation

  1. User enters N.
  2. Loop runs from i = 1 to N.
  3. In each iteration, the i-th even number 2*i is added to sum.
  4. After the loop ends, the total sum is printed.

 

C Program: Print sum of first N even numbers

Method 2: Using while loop

A while loop can be used to iteratively add the first N even numbers.
The i-th even number is given by 2 * i.

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;  // Add ith even number

        i++;           // Increment counter

    }

 

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

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 7
Sum of first 7 even numbers = 56

OUTPUT 2 :
Enter the value of N: 5
Sum of first 5 even numbers = 30

Explanation

  1. Initialize i = 1 and sum = 0.
  2. Loop continues while i <= N.
  3. In each iteration, add 2 * i to sum.
  4. Increment i by 1.
  5. Print the total sum after the loop ends.

 

C Program: Print sum of first N even numbers

Method 3: Using do..while loop

A do-while loop executes the loop body at least once, which is perfect for calculating sums.
The i-th even number is 2 * i. This program will add the first N even numbers iteratively.

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;  // Add ith even number

        i++;           // Increment counter

    } while (i <= N);

 

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

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 6
Sum of first 6 even numbers = 42

OUTPUT 2 :
Enter the value of N: 5
Sum of first 5 even numbers = 30

Explanation

  1. Initialize i = 1 and sum = 0.
  2. Do block executes first: adds 2*i to sum.
  3. Increment i by 1.
  4. Check condition i <= N. If true, repeat.
  5. Loop stops when i > N and prints the total sum.