C Program to Print Even Numbers Between 1 to 100 using For and While Loop | How to Write a C Program that Prints List of Even Numbers up to 100?

Wondering How to Print Even Numbers between 1 to 100 using for Loop as well as While Loop? If so, don’t bother as ou tutorial completely describes how to write C Program that Prints Even Numbers between 1 to 100 using both for and while loops. You can simply check the sample programs over here and understand the logic to create one on your own. To learn the program effectively you need to be aware of the following

Even Numbers – Definition

Even Numbers are the numbers that are exactly divisible by 2. Even Numbers always end with digits 0, 2, 4, 6, 8. Smallest Positive Even Natural Number is 2.
Examples of Even Numbers are 2, 14, 16, 20, etc.

Write a C Program to Print Even Numbers Between 1 to 100 using For Loop?

This program will print the even numbers between 1 to 100 by using the for loop.

C Program to Print Even Numbers Between 1 to 100 using For and While Loop

#include <stdio.h>  
   
int main() {  
    int counter; 
    printf("Even numbers between 1 to 100\n");  
    /* 
     * Initialize counter with 1, and increment it in every iteration.
     * For every value of counter check whether it is even number or
     * not and print it accordingly 
     */ 
    for(counter = 1; counter <= 100; counter++) {  
        /* Even numbers are divisible by 2 */ 
        if(counter%2 == 0) { 
            /* counter is even, print it */
            printf("%d ", counter);  
        }  
    }  
   
    return 0;  
}

Logic:

  • Here we have declared the variable counter in the initial step
  • Later on, we have initialized the counter value by 1.
  • Using for loop we will check the condition counter <=100 and if it is met loop will execute by further checking the condition counter%2==0 or not to determine if the number is divisible by 2 or not. If it is true counter value is incremented and the loop iterates as such to get all the even numbers between 1 to 100.

Output

Even numbers between 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

Write a C Program to Print Even Numbers from 1 to 100 using While Loop?

The intent of the program is the same as above it is just that we have replaced the for loop with while loop to print the even numbers between 1 to 100. You can use the below program to find out all even numbers between 1 to n using while loop by simply declaring one more variable and enter the limit.

C program to print even numbers from 1 to N using while loop

#include <stdio.h>  
   
int main() {  
    int counter; 
    printf("Even numbers between 1 to 100\n");  
    /* 
     * Initialize counter with first even number 2, and increment 
     * it by 2 in every iteration.
     */
    counter = 2;
    while (counter <= 100) {  
        printf("%d ", counter);
        /* Add 2 to current even number 
           to get next even number */
        counter = counter + 2;  
    }  
   
    return 0;  
}

Logic:

  • Here a counter variable is declared in the above program i.e. is the loop counter
  • Initialize the loop counter by 2 as it is the first even number value
  • Use the while loop to check the counter number <=100 and execute the loop till the value of a number is less than or equal to 100.
  • If the condition is met then the loop executes and the counter value is incremented. the same is done till we get the even numbers between 1 to 100.

Output:

Even numbers between 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