C Program To Print Even and Odd Numbers From 1 To 100
In this tutorial, we are going to see how to write a program to print even and odd numbers from 1 to 100 in C language using two version: for and while loop.
Before diving into the code, let’s quickly revisit the definitions:
- Even numbers are divisible by 2 (e.g., 2, 4, 6, 8…).
- Odd numbers are not divisible by 2 (e.g., 1, 3, 5, 7…).
C Program To Print Even and Odd Numbers From 1 To 100 using FOR loop
In the following C code We use a for loop from 1 to 100. The if(i % 2 == 0) condition checks whether the number is even. If true, we print the number. To identify odd numbers we check if(i % 2 != 0).
#include <stdio.h>
int main() {
int i; // Declare loop variable
// Print Even Numbers from 1 to 100
printf("Even numbers from 1 to 100:\n");
for(i = 1; i <= 100; i++) {
if(i % 2 == 0) { // Check if 'i' is divisible by 2 (even)
printf("%d ", i); // Print the even number
}
}
printf("\n\nOdd numbers from 1 to 100:\n");
// Print Odd Numbers from 1 to 100
for(i = 1; i <= 100; i++) {
if(i % 2 != 0) { // Check if 'i' is not divisible by 2
printf("%d ", i); // Print the odd number
}
}
return 0; // Indicate successful program termination
Output:
Even numbers from 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 Odd numbers from 1 to 100 : 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
C Program To Print Even and Odd Numbers From 1 To 100 using WHILE loop
This C program demonstrates how to print even and odd numbers from 1 to 100 using while loops. It initializes the variable i to 2 for even numbers and increments it by 2 in each iteration until it reaches 100. Then, it resets i to 1 and uses the same logic to print all odd numbers by adding 2 in each loop iteration. This method avoids the use of conditional statements and relies on simple arithmetic to separate even and odd numbers efficiently.
#include <stdio.h>
void main()
{
int i,last=100;
//While Loop
//Code For Even Number List
printf("\nEven Number List :\n ");
i=2;
while(i <= last)
{
printf(" %d",i);
i = i + 2;
}
//Code For Odd Number List
printf("\nOdd Number List :\n ");
i=1;
while(i <= last)
{
printf(" %d",i);
i = i + 2;
}
}
Output:
Even Number List : 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 Odd Number List : 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99




