C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Print first N even numbers

Introduction

Even numbers are integers that are exactly divisible by 2 — for example, 2, 4, 6, 8, 10, etc.
This program takes an integer N from the user and prints the first N even numbers using a loop.

Formula for the i-th even number is:

Even Number = 2 × i

C Program: Print 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;

 

    // Input

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

    scanf("%d", &N);

 

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

 

    // Loop to print even numbers

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

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

    }

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 10
First 10 even numbers are:
2 4 6 8 10 12 14 16 18 20

OUTPUT 2 :
Enter the value of N: 5
First 5 even numbers are:
2 4 6 8 10

Explanation

  1. User enters a value for N.
  2. The loop runs from 1 to N.
  3. Each iteration prints 2 * i, which gives the even number.
  4. Output displays the first N even numbers in sequence.

 

C Program: Print first N even numbers

Method 2: Using while loop

In this version, we’ll use a while loop to print the first N even numbers.
Since even numbers are multiples of 2 (2, 4, 6, 8, ...), we can print them using a simple counting logic.

 

C

#include <stdio.h>

 

int main() {

    int N, i = 1;

 

    // Input

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

    scanf("%d", &N);

 

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

 

    // While loop

    while (i <= N) {

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

        i++;  // Increment counter

    }

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 8
First 8 even numbers are:
2 4 6 8 10 12 14 16

OUTPUT 2 :
Enter the value of N: 5
First 5 even numbers are:
2 4 6 8 10

Explanation

  1. Initialize counter i = 1.
  2. Loop continues as long as i <= N.
  3. In each iteration, print 2 * i (the even number).
  4. Increment i by 1 after printing.
  5. When i > N, the loop stops.

 

C Program: Print first N even numbers

Method 3: Using do..while loop

The do-while loop ensures that the loop body executes at least once, even if the condition is false initially.
We’ll use it to print the first N even numbers, where each even number is calculated as 2 × i.

 

C

#include <stdio.h>

 

int main() {

    int N, i = 1;

 

    // Input

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

    scanf("%d", &N);

 

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

 

    // Do-while loop

    do {

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

        i++;

    } while (i <= N);

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 7
First 7 even numbers are:
2 4 6 8 10 12 14

OUTPUT 2 :
Enter the value of N: 3
First 3 even numbers are:
2 4 6

Explanation

  1. User enters a number N.
  2. The do block runs first, printing 2 × i.
  3. Then i is incremented by 1.
  4. The loop continues while i <= N.
  5. When i becomes greater than N, it stops.