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. How to display even numbers between 1 to n using for loop in C programming. Logic to print even number using if else and for loop in given range in C program.

Example

Input

Input upper range: 10

Output

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

Required knowledge

Basic C programming, If else, For loop

There can be various approaches to print even numbers in C. Here I am explaining the two common and beginner methods.

Logic to print even numbers using if condition

This is the first most chosen method to print even numbers. Before I formally get to the logic, below are two prerequisite for this logic.

Read more -

Let us get to the logic to print even number using if condition.

  1. Input upper limit of the even number to be printed from user. Store it in some variable say N.
  2. Initialize a loop from 1, that runs till N, incrementing 1 in each iteration. The loop structure should look like for(i=1; i<=N; i++).
  3. Inside the loop body check the even condition. If the current number i.e. i is even. Which is 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;
  
    /* Read 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;
}

As I spoke earlier, let us see the other approach to print even numbers. However, before we move on to the other method, you must question why other? What's the problem with above one? The above method is simple and easiest to implement. But it is not the optimal way for the given problem statement. We unintentionally iterating over all natural numbers between 1 to n. We intended only to print even numbers.

Then how to do so? Let us now talk other logic to print even numbers.

Logic to print even numbers without if statement

Below is the step by step descriptive logic to print even numbers from 1 to n.

  1. Read upper limit to print even number from user. Store it in some variable say n.
  2. This time run a loop from 2, that goes till n, incrementing 2. So the loop structure looks like for(i=2; i<=n; i+=2).
    Why started from 2? Why not, as we need to print even numbers from 1 to n. Hence the first even number is 2. So why not, start with 2 instead of 1.

    Why incrementing the loop by 2 i.e. i+=2, not by 1 i.e. using i++? Oh! come on a kid can answer. If i is even number, then to get next even we need to add 2, not 1.

  3. Finally inside loop body print the value of i.

Program to display 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, 
     * for each repetition
     */
    for(i=2; i<=n; i+=2)
    {
        printf("%d\n",i);
    }

    return 0;
}

Note: Instead of i+=2, you can also use i = i + 2. Both convey the same meaning.

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 the program to print even number in a given range.

Program to print even number in range

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

#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 some other approaches to solve this program.

Read 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 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>