C Program to Print Even Numbers from 1 to N Using While Loop

Hello readers, today we will learn how to write a program to print even numbers from 1 to N using while loop in C Programming language.

C Program to Print Even Numbers from 1 to N Using While Loop

This program asks the user to enter a number up to which the user wants the even numbers. Then, it will print all even numbers lying between 1 to n using while loop.

So, without any delay, let’s begin this tutorial.

C Program to Print Even Numbers from 1 to N Using While Loop

C Program

// C Program to Print Even Numbers From 1 to N Using While Loop
#include <stdio.h>
int main(){
    int i, num;
    
    // Asking for input
    printf("Enter a positive number: ");
    scanf("%d", &num);
    
    printf("All even numbers from 1 to %d: ", num);
    i = 1;
    while (i <= num){
        if (i % 2 == 0){
            printf("%d\t", i);
        }
        i++;
    }
    return 0;
}

Output

Enter a positive number: 22
All even numbers from 1 to 22: 2	4	6	8	10	12	14	16	18	20	22	
C Program to Print Even Numbers from 1 to N Using While Loop

Explanation

    int i, num;

In this program, we have declared two int data type variables named i and num.

    // Asking for input
    printf("Enter a positive number: ");
    scanf("%d", &num);

Then, the user is asked to enter a number up to which the user wants to display even numbers.

    while (i <= num){
        if (i % 2 == 0){
            printf("%d\t", i);
        }
        i++;
    }

We iterate the loop with i = 1. For each iteration, we check whether i is divisible by 2 or not, if yes then we print that value of i and increment the value of i by 1.

This process continues until i is less than or equal to num. When i > num, then the loop terminates and the program exits.

C Program to Print Even Numbers Using While Loop

// C Program to Print Even Numbers From 1 to N Using While Loop
#include <stdio.h>
int main(){
    int i, num;
    
    // Asking for input
    printf("Enter a positive number: ");
    scanf("%d", &num);
    
    // Printing even numbers from 1 to N
    printf("All even numbers from 1 to %d: \n", num);
    i = 2;
    while (i <= num){
        printf("%d\t", i);
        i = i + 2;
    }
    return 0;
}

Output

Enter a positive number: 50
All even numbers from 1 to 50: 
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	

Explanation

    printf("All even numbers from 1 to %d: \n", num);
    i = 2;
    while (i <= num){
        printf("%d\t", i);
        i = i + 2;
    }

In this program, the iteration starts from i = 2. For each iteration, we print the value of i and then increment the i by 2. This gives us the even numbers from 1 to num.

This process continues until i <= num.

Conclusion

I hope after reading this post, you understand how to write a program to print even numbers from 1 to N using while loop in C Programming language.

If you have any doubt regarding the program, let us know in the comment section. We will be delighted to solve your query. 

Also Read:

Leave a Comment