C Program to Print Even Numbers from 1 to 100

C program to print even numbers from 1 to 100 using loops.


Problem

Write a program to display even numbers from 1 to 100 using a loop of your choice.


Expectations:

You need to write the complete program using the following template as the starter:

#include <stdio.h>

int main() {
    // Write your code here.
    
    return 0;
}

Copy and paste the above code in your favorite IDE and start writing your code after the comment “// Write your code here.

Pro tip:  First, try writing the code on your own. Try every possible thing you can to come up with the solution. If you still can’t figure it out, then check the solution and see what went wrong. This is how you learn. 🙂


Solution

Following are the multiple approaches to solve the given problem:

The for loop with if statement

We can use a for loop to get numbers from 1 to 100 and for each number, we can check the condition whether the number is divisible by 2. If yes, then we will print it as it is an even number. The equivalent program is as follows:

#include <stdio.h>

int main()
{
    for (int n = 1; n <= 100; n++) {
        if (n % 2 == 0)
            printf("%d ", n);
    }

    return 0;
}

Output:

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

The for loop without the if statement

The if check within the loop is not necessary. Instead, the following code works just fine without the if statement:

#include <stdio.h>

int main()
{
    for (int n = 2; n <= 100; n+=2) {
        printf("%d ", n);
    }

    return 0;
}

Output:

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

Notice that the loop variable is initialized to 2 (not 1) and instead of increment by 1, we are now performing increment by 2 on the loop variable. This allows us to skip all odd numbers between 1 to 100.

The while loop

The while loop implementation is pretty straightforward. Observe the following code:

#include <stdio.h>

int main()
{
    int n = 2;
    while (n <= 100) {
        printf("%d ", n);
        n += 2;
    }

    return 0;
}

Output:

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

The do-while loop

The do-while loop implementation is as follows:

#include <stdio.h>

int main()
{
    int n = 2;
    do {
        printf("%d ", n);
        n += 2;
    } while (n <= 100);

    return 0;
}

Output:

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


Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.