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 a given range. How to display even numbers between 1 to n using for loop in C programming.

Example:
Input range: 10
Output even numbers between 1 to 10: 2, 4, 6, 7, 10

Also check this program - using while loop C program to print even numbers from 1 to 100 using while loop.

Required knowledge

Basic C programming, If else, For loop

Even number:

Any number which is exactly divisible by 2 is called as even number. For example the first 5 even numbers are 2, 4, 6, 8, 10.

Before moving to this program one more thing you must be done with is you must know how to check whether a number is even or not using if else.

Program to print even numbers using if

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

#include <stdio.h>

int main()
{
    int i, n;
  
    //Reads the 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);

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

    return 0;
}

Note: The above method of generating even numbers is less optimal and not recommended. You can also write the same program without using if else as I have done in below program which is 2 times faster than the above program.

Program to display even numbers without using if statement

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

#include <stdio.h>

int main()
{
    int i, n;

    //Reads the 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);

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

    return 0;
}


Output
X
_
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


Happy coding ;)


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
< code >< pre > -- Your source code -- < /pre >< /code > (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