C program to print all even numbers between 1 to n using while loop

Write a C program to enter any number from user and print all even numbers between 1 to n using while loop. C program to display even number in a given range using while loop. How to generate even numbers from 1 to n using while loop in C programming.

Example:
Input value of n: 10
Output even numbers between 1 to 10: 2, 4, 6, 8, 10

Also do check this program using for loop - C program to print all even numbers between 1 to n using for loop.

Required knowledge

Basic C programming, If else, While loop

Even numbers

Numbers that are exactly divisible by 2 are called as even numbers. Example: 2, 4, 6, 8, 10 are the first 5 even numbers.

Before moving to this program you must know how to check whether a number is even or odd 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
     */
    i=1;
    while(i<=n)
    {
        /* Check even condition before printing */
        if(i%2==0)
        {
            printf("%d\n", i);
        }

        i++;
    }

    return 0;
}

Note: As I already mentioned the above method of generating even numbers is not optimal and takes more time to execute. Hence you must use the below method to generate even numbers.

Program to display even numbers without using if statement

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

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

    i=2; //Since 2 is the first even number
    while(i<=n)
    {
        printf("%d\n", i);

        /*
         * Adding 2 to current even number will give next even number
         */
        i += 2;
    }

    return 0;
} 


Output
X
_
Print all even numbers till: 100
All even numbers from 1 to 100:
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).

3 comments:

  1. (by loop plz ans me soon)
    Print the sequence of all Even numbers within the range input by user
    Print the sequence of all Odd numbers within the range input by user
    Write a program that printout FIBONACCI SERIE ( 1, 1, 2, 3, 5, 8, 13.....).
    Print the Sequence [8 12 17 24 28 33 upto 100]
    Write a program to that read the non negative integers & compute & prints its factorial. (5! = 5*4*3*2*1 = 120).
    Print the Sequence [64 32 16 8 4 2]
    Print the Sequence [1 3 9 27 81 upto 200]
    Write a program that calculates & prints the average of several integers. Assume the last value read is sentinel 9999. A typical input sequence might be. 10 8 6 7 2 9999 indicating that the average of all the values preceding 9999 is to be calculated. (using while and do-while)
    Write a program that determines the sequence of Prime Numbers within the range input by user
    Calculate the class average program with Sentinel-controlled Repetition using While Loop. (using while and do-while)
    Write a program to summarize the result of a test. You have to ask the number of students to be tested. Next to each result, is written a 1 if the student passed the exam and 2 if the student fail. Then generate the number of students pass & fail. (using while and do-while)
    Write a program that sums the sequence of integers. Assuming that the first integer read specifies the number of values remaining to be entered. Program should read only one value per input statement. A typical input sequence mighWhere the 5 indicates that the subsequent 5 values are to be summed

    t be. 5 100 200 150 300 500

    ReplyDelete
  2. (by loop plz ans me soon)
    Print the sequence of all Even numbers within the range input by user
    Print the sequence of all Odd numbers within the range input by user
    Write a program that printout FIBONACCI SERIE ( 1, 1, 2, 3, 5, 8, 13.....).
    Print the Sequence [8 12 17 24 28 33 upto 100]
    Write a program to that read the non negative integers & compute & prints its factorial. (5! = 5*4*3*2*1 = 120).
    Print the Sequence [64 32 16 8 4 2]
    Print the Sequence [1 3 9 27 81 upto 200]
    Write a program that calculates & prints the average of several integers. Assume the last value read is sentinel 9999. A typical input sequence might be. 10 8 6 7 2 9999 indicating that the average of all the values preceding 9999 is to be calculated. (using while and do-while)
    Write a program that determines the sequence of Prime Numbers within the range input by user
    Calculate the class average program with Sentinel-controlled Repetition using While Loop. (using while and do-while)
    Write a program to summarize the result of a test. You have to ask the number of students to be tested. Next to each result, is written a 1 if the student passed the exam and 2 if the student fail. Then generate the number of students pass & fail. (using while and do-while)
    Write a program that sums the sequence of integers. Assuming that the first integer read specifies the number of values remaining to be entered. Program should read only one value per input statement. A typical input sequence mighWhere the 5 indicates that the subsequent 5 values are to be summed

    t be. 5 100 200 150 300 500

    ReplyDelete