C program to print all even numbers from 1 to n

Previous Program Next Program

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.

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

  1. Read 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. But 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 about that.

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). Let us check the loop details.

    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+=2, not by 1 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;

    //Read 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;

    //Read 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.

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 ;)

You may also like

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

8 comments:

  1. I did it like this

    #include
    main()
    {
    int i=1;
    while(i<=100)
    {
    i++;
    if(i%2==0)
    {

    printf("Even numbers from 1 to 100 is %d\n", i);

    }


    }
    system("pause");
    }

    ReplyDelete
    Replies
    1. The code is correct @Gaurav but here's a small advice.

      Instead of using if statement you can simply print it as I have mentioned in my second program. As it is more efficient.

      Delete
  2. Like this

    #include
    main()
    {
    int j=0;
    while(j<100)
    {
    j+=2;
    printf("Even numbers from 1 to 100 is %d\n", j);
    }
    system("pause");
    }

    ReplyDelete
    Replies
    1. Yes @Gaurav this is more efficient way to do this program.

      Delete
  3. kindly write for me a program @pankaj prakash that will display the square of all even numbers between 0 and 100

    ReplyDelete
    Replies
    1. You can use this program. You just need to make changes in the printf() statement inside the for loop. Use printf("%d\n", i*i); to print square of all even numbers.

      Delete
  4. hey man can you count the number of results in a code. lets say the even numbers from 1 to 10 are 2,4,6,8,10 and the total result is 5. How do you make the code print that total result ?

    ReplyDelete
    Replies
    1. You can do that simply by replacing printf() inside for loop with a count variable like

      #include <stdio.h>

      int main()
      {
      int i, count=0;

      printf("All even numbers from 1 to 100 are: \n");

      for( i=2 ; i<=100 ; i+=2 )
      {
      count++;
      }

      printf(" Total no of even no =℅d", count);

      return 0;
      }

      Delete