C Program to Print Even Numbers From 1 to N

Howdy readers, today you will learn how to write a program to print even numbers from 1 to N Using the C Programming language.

C Program to Print Even Numbers From 1 to N

Any number which is exactly divisible by 2 is called an Even number. For example: 4, 32, 512, 1248, . . . , etc.

We will print all even numbers from 1 to N using the following methods:

  1. Using For Loop
  2. Using While Loop
  3. Without Using If Statement
  4. In a Range

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

C Program to Print Even Numbers From 1 to N

C Program

// C Program to Print Even Numbers From 1 to N Using For Loop
#include <stdio.h>
int main(){
    int num;
    
    // Taking input
    printf("Enter the maximum limit: ");
    scanf("%d", &num);
    
    printf("Even numbers from 1 to %d are: \n", num);
    for (int i = 1; i <= num; i++){
        if (i % 2 == 0){
            printf("%d\t", i);
        }
    }
    return 0;
}

Output

Enter the maximum limit: 25
Even numbers from 1 to 25 are: 
2	4	6	8	10	12	14	16	18	20	22	24	
C Program to Print Even Numbers From 1 to N Using For Loop

Explanation

    int num;

We have declared an int data type variable named num.

    // Taking input
    printf("Enter the maximum limit: ");
    scanf("%d", &num);

The user is asked to enter the maximum limit up to which the user wants to print even numbers. This value gets stored in the num named variable.

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

For each iteration, we check whether that iteration of i is exactly divisible by 2 or not. If it is exactly divisible by 2, then it is an even number and we print it on the display using printf() function.

The \t is an escape sequence used to give tab spaces in the C Programming language.

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 = 2, num;
    
    // Taking input from the user
    printf("Enter the maximum limit: ");
    scanf("%d", &num);
    
    printf("Even numbers from 1 to %d are: \n", num);
    while (i <= num){
        if (i % 2 == 0){
            printf("%d\t", i);
        }
        i++;
    }
    
    return 0;
}

Output

Enter the maximum limit: 15
Even numbers from 1 to 15 are: 
2	4	6	8	10	12	14
C Program to Print Even Numbers From 1 to N Using While Loop

Explanation

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

The logic to check even numbers is the same as that of the above program, we have just used a while loop instead of for loop in this program.

C Program to Print Even Numbers From 1 to N Without Using If Statement

C Program

// C Program to Print Even Numbers From 1 to N Without Using If Statement
#include <stdio.h>
int main(){
    int num;
    
    // Asking for input
    printf("Enter the upper limit: ");
    scanf("%d", &num);
    
    printf("Even numbers from 1 to %d are: \n", num);
    for (int i = 2; i <= num; i = i + 2){
        printf("%d\t", i);
    }
    
    return 0;
}

Output

Enter the upper limit: 50
Even numbers from 1 to 50 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	
C Program to Print Even Numbers From 1 to N Without Using If Statement

Explanation

    for (int i = 2; i <= num; i = i + 2){
        printf("%d\t", i);
    }

In this program, our iteration starts with i = 2 and prints the value of 2. After each iteration, the value of i is incremented by 2. By this way, we get all the even numbers without using the if statement

C Program to Print Even Numbers in a Range

C Program

// C Program to Print Even Numbers in a Given Range
#include <stdio.h>
int main(){
    int min, max;
    
    // Taking input
    printf("Enter the minimum value: ");
    scanf("%d", &min);
    printf("Enter the maximum value: ");
    scanf("%d", &max);
    
    printf("Even numbers from %d to %d are: \n", min, max);
    for (int i = min; i <= max; i++){
        if (i % 2 == 0){
            printf("%d\t", i);
        }
    }
    
    return 0;
}

Output

Enter the minimum value: 12
Enter the maximum value: 27
Even numbers from 12 to 27 are: 
12	14	16	18	20	22	24	26	
C Program to Print Even Numbers From 1 to N in a Range

Explanation

    printf("Enter the minimum value: ");
    scanf("%d", &min);
    printf("Enter the maximum value: ");
    scanf("%d", &max);

In the above program, the user is asked to enter the minimum and maximum value of the range. These values get stored in the min and max named variables.

    for (int i = min; i <= max; i++){
        if (i % 2 == 0){
            printf("%d\t", i);
        }
    }

The logic is the same as that of the first program. Here, we have just initiated the loop with i = min.

Conclusion

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

If you have any queries regarding the tutorial, then let us know in the comment section. We will be pleased to solve all of your queries.

Leave a Comment