C program to print all even numbers from 1 to n

Write a C program to print all even numbers from 1 to n using for loop. C program to generate all even numbers between given range. Logic to print even numbers using if else and for loop in given range in C programming.

Example

Input

Input upper range: 10

Output

Even numbers between 1 to 10:
2, 4, 6, 8, 10

Required knowledge

Basic C programming, Relational operators, If statement, For loop

There are various approaches to print even numbers in given range. Here in this post I will explain two common beginner methods to print even numbers.

Logic to print even numbers using if condition

First let us talk about the easiest way to print even numbers. If I ask you to list all even numbers from 1 to 100 what will be your immediate step? You will probably start from 1 check if its even number then add it to the even list otherwise not.

Step by step descriptive logic to print all even number between 1 to n using if condition.

  1. Input upper limit to the even numbers from user. Store it in some variable say N.
  2. Run a loop from 1, that runs till N, increment the loop counter by 1 in each iteration. The loop structure should look like for(i=1; i<=N; i++).
  3. Inside the loop body check even/odd condition. If the current number i is divisible by 2 then i is even. Means if(i % 2 == 0), then print the value of i.

Program to print even numbers using if condition

/**
 * C program to print all even numbers from 1 to n
 */

#include <stdio.h>

int main()
{
    int i, n;
  
    /* Input upper limit of even number from user */
    printf("Print all even numbers till: ");
    scanf("%d", &n);

    printf("Even numbers from 1 to %d are: \n", n);

    /*
     * Start loop counter from 1, increment it by 1,
     * will iterate till n
     */
    for(i=1; i<=n; i++)
    {
        /* Check even condition before printing */
        if(i%2 == 0)
        {
            printf("%d\n", i);
        }
    }

    return 0;
}

Logic to print even numbers without if statement

The above approach to list even numbers is not optimal. It unnecessarily iterates for odd numbers which is a performance issue. To overcome this start the loop with first even number. We know if n is an even number then n + 2 is the next even number. Hence, to get next even number just add 2 to the current even number.

Step by step descriptive logic to print even numbers from 1 to n without using if statement.

  1. Input upper limit to print even number from user. Store it in some variable say n.
  2. Run a loop from first even number i.e. 2 (in this case), that goes till n and increment the loop counter by 2 in each iteration. So the loop structure looks like for(i=2; i<=n; i+=2).
  3. Finally inside loop body print the value of i.

Program to print even numbers without using if statement

/**
 * C program to display all even numbers from 1 to n without if
 */

#include <stdio.h>

int main()
{
    int i, n;

    /* Input upper limit of even number from user */
    printf("Print all even numbers till: ");
    scanf("%d", &n);

    printf("All even numbers from 1 to %d are: \n", n);

    /*
     * Start loop from 2 and increment by 2, 
     * in each repetition
     */
    for(i=2; i<=n; i+=2)
    {
        printf("%d\n",i);
    }

    return 0;
}

Note: In the above code I have used shorthand assignment operator i+=2 which is equivalent to i = i + 2.

Output

Print all even numbers till: 100
All even numbers from 1 to 100 are: 
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100

Finally let us write program to print even number in a given range.

Program to print even numbers in given range

/**
 * C program to display all even numbers in given range
 */

#include <stdio.h>

int main()
{
    int i, start, end;

    /* Input upper and lower limit */
    printf("Enter lower limit: ");
    scanf("%d", &start);
    printf("Enter upper limit: ");
    scanf("%d", &end);

    printf("All even numbers from %d to %d are: \n", start, end);

    /* If 'start' is not even make it even */
    if(start%2 != 0)
    {
        start++;
    }

    /*
     * Initialize loop from start and increment by 2, 
     * for each repetition
     */
    for(i=start; i<=end; i+=2)
    {
        printf("%d\n",i);
    }

    return 0;
}

Before moving on to next exercise or program. Enhance your skill and learn other approach to solve this program.

Learn more – Program to print even numbers using while loop.

Output

Enter lower limit: 40
Enter upper limit: 50
All even numbers from 40 to 50 are: 
40
42
44
46
48
50

Happy coding 😉