C program to print given number is even or odd using for loop
Understanding Even and Odd Numbers
Before diving into the code, let’s establish a clear understanding of even and odd numbers. An even number is any integer that can be divided by 2 with no remainder. In simpler terms, an even number is perfectly divisible by 2. Conversely, an odd number is any integer that leaves a remainder when divided by 2.
For example, 4, 8, 10, and 12 are even numbers, while 3, 5, 7, and 9 are odd numbers.
Checking Evenness with Modulus Operator
In C, the %
operator performs the modulus operation. The modulus of two numbers returns the remainder after dividing the first number by the second. Therefore, checking if a number is even boils down to verifying if the remainder of dividing it by 2 is 0.
For example, if we divide 6 by 2, the result is 3 with a remainder of 0. This means 6 is an even number. Conversely, dividing 5 by 2 gives a result of 2 with a remainder of 1. Hence, 5 is an odd number.
Building the For Loop
Now that we understand evenness and the modulus operator, let’s create a for loop to iterate through a range of numbers and determine their evenness. Here’s the basic structure:
C
for (int i = start; i <= end; ++i) {
// Check if i is even
// Print whether i is even or odd
}
In this structure:
i
is the loop counter variable, taking values fromstart
toend
(inclusive).- The
++i
at the end increments the counter after each iteration. - The inner block checks if
i
is even and then prints an appropriate message.
Checking for Evenness Inside the Loop
Within the loop, we need to check if the current value of i
is even. As discussed earlier, we can use the modulus operator:
C
if (i % 2 == 0) {
// i is even
printf("%d is even\n", i);
} else {
// i is odd
printf("%d is odd\n", i);
}
This code snippet uses an if
statement to check if the remainder of dividing i
by 2 is 0. If it is, i
is even, and the program prints a message confirming this. Otherwise, i
is odd, and the appropriate message is printed.
Putting it All Together
Combining the loop structure and the evenness check, we get the complete C program:
C
#include <stdio.h>
int main() {
int start, end;
// Get the range of numbers from the user
printf("Enter the starting number: ");
scanf("%d", &start);
printf("Enter the ending number: ");
scanf("%d", &end);
// Loop through the range and check each number
for (int i = start; i <= end; ++i) {
if (i % 2 == 0) {
printf("%d is even\n", i);
} else {
printf("%d is odd\n", i);
}
}
return 0;
}
This program first prompts the user for the starting and ending numbers of the range. Then, it iterates through this range using a for loop. For each number, the program checks its evenness using the modulus operator and prints the corresponding message.
Conclusion
This C program demonstrates how to use a for loop and the modulus operator to check if a number is even or odd. You can expand on this code by:
- Modifying the code to check only for even or odd numbers instead of printing both.
- Adding functionality to count the number of even and odd numbers in the given range.
- Adapting the program to check whether a specific user-inputted number is even or odd.
By understanding the principles behind this program, you can build upon it and develop more complex solutions to analyze and manipulate numbers in C.