You are currently viewing C Program To Print All Even Numbers Between 1 to 100 Using While Loop

C Program To Print All Even Numbers Between 1 to 100 Using While Loop

In this tutorial, you will learn how to print all even numbers between 1 to 100 using while loop conditional statement in c. In this article, we will go through examples, logic, algorithms, and program explanations for your better understanding.

Required Knowledge

Problem Statement

You have to write a C program that prints all the even numbers between 1 and 100 using a while loop. The program should take an integer input from the user and print all the even numbers within a specified range using a while loop. You can refer our below example.

Example

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

Algorithm

  1. Start the program.
  2. Declare a variable num to represent the current number, initialized to 1.
  3. Print the header message: “Even numbers between 1 and 100:”
  4. Enter the while loop with the condition num <= 100.
    • Check if the current number num is even using the modulo operator (num % 2 == 0).
    • If it is even, print the value of num.
    • Increment num by 1.
  5. Exit the while loop.
  6. Print a new line to separate the output.
  7. End the program.

Logic used to solve this problem

  • Initialize a num variable to 1.
  • Use a while loop that runs as long as the num is less than or equal to 100.
  • Inside the loop, check if the num is an even number using the modulo (%) operator.
  • If the num is even, print the number.
  • Increment the num by 1 in each iteration.
  • Continue the loop until the counter reaches 100.
  • Finish the program.

Program to Print all even numbers between 1 to 100

#include <stdio.h>
int main() {
    int num = 1;
    printf("Even numbers between 1 and 100: ");
    while (num <= 100) {
        if (num % 2 == 0) {
            printf("%d ", num);
        }
        num++;
    }
    printf("\n");
    return 0;
}

Output

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

Program Explanation

  • The program initializes a variable num to 2, which represents the first even number to be printed.
  • The program enters a while loop with the condition num <= 100 to iterate through numbers from 2 to 100.
  • Inside the while loop, it prints the value of num on the screen.
  • It then increments the value of num by 2 since the next even number will be 2 greater than the current one.
  • The loop continues until num exceeds the value of 100.
  • Once the while loop is complete, the program ends.

Conclusion

The C program successfully prints all the even numbers between 1 and 100 using a while loop. By checking the divisibility of each number by 2, it identifies the even numbers and prints them on a single line. I hope this program is clear to you.