Determining whether a number is even or odd is one of the fundamental operations in programming, especially in C. This concept is widely used in various applications such as filtering data, performing conditional logic, and creating algorithms. In this article, we will explore three methods to write a C program to check whether a number is even or odd, complete with explanations, code examples, and outputs. You can also refer to our complete guide to learn C Program to Check Whether a Number is Positive or Negative.
C Program to Check Whether a Number is Even or Odd: A Complete guide
What Is an Even or Odd Number?
- Even Number: A number divisible by 2 without leaving a remainder. For example, 2, 4, 6, and 8.
- Odd Number: A number that leaves a remainder of 1 when divided by 2. For example, 1, 3, 5, and 7.
Method 1) C Program to Check Whether a Number is Even or Odd Using the Modulus Operator
Code Example
#include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (num % 2 == 0) { printf("%d is even.\n", num); } else { printf("%d is odd.\n", num); } return 0; }
Explanation
The modulus operator (%
) is the simplest way to check whether a number is even or odd. When you divide a number by 2:
- If the remainder is 0, the number is even.
- If the remainder is 1, the number is odd.
Output
Input: 8 Output: 8 is even. Input: 15 Output: 15 is odd.
Method 2) C Program to Check Whether a Number is Even or Odd Using the Bitwise AND Operator
Explanation
Every integer is represented in binary. The least significant bit (LSB) determines if a number is even or odd:
- Even numbers have
0
as the LSB. - Odd numbers have
1
as the LSB.
The bitwise AND operator (&
) checks the LSB:
num & 1
returns 0 for even numbers.num & 1
returns 1 for odd numbers.
Code Example
#include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (num & 1) { printf("%d is odd.\n", num); } else { printf("%d is even.\n", num); } return 0; }
Output
Input: 4 Output: 4 is even. Input: 9 Output: 9 is odd.
Why Use This Method?
This method is efficient and avoids the need for division operations, making it ideal for performance-critical applications like embedded systems.
Method 3) C Program to Check Whether a Number is Even or Odd Using Division and Multiplication
Explanation
Divide the number by 2, then multiply the quotient back by 2:
- If the result matches the original number, it is even.
- Otherwise, it is odd.
Code Example
#include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); if ((num / 2) * 2 == num) { printf("%d is even.\n", num); } else { printf("%d is odd.\n", num); } return 0; }
Output
Input: 10 Output: 10 is even. Input: 7 Output: 7 is odd.
Why Use This Method?
This method demonstrates the concept of even and odd numbers without directly using the modulus operator, making it a good educational tool.
Frequently Asked Questions (FAQs)
How to write a program to check if a number is even or odd?
You can use the modulus operator (%
), bitwise AND operator (&
), or division and multiplication logic to check if a number is even or odd.
How to check odd and even numbers in C?
Use conditionals (if-else
) with the modulus or bitwise AND operator to determine whether a number is even or odd.
How do you write whether the number is odd or even?
Use if (num % 2 == 0)
for even numbers or if (num % 2 != 0)
for odd numbers.
How to write an odd number program in C?
Use the modulus operator (num % 2
) or bitwise AND (num & 1
) to check if a number is odd.
How do you know if a number is odd or even?
A number is even if divisible by 2 without remainder, and odd if not.
How to check if a number is even or odd without modulus?
Use the bitwise AND operator (num & 1
) or compare the result of (num / 2) * 2
with the original number.
What is the algorithm for odd or even numbers in C?
- Input a number.
- Use
if (num % 2 == 0)
or equivalent logic. - Print whether the number is even or odd.
How do you code if a number is even or odd?
Choose one of the three methods: modulus, bitwise AND, or division and multiplication.
Is zero even or odd?
Zero is even because it is divisible by 2 without a remainder.
How to find odd and even numbers in a range?
Iterate through the range using a loop, and use modulus or bitwise AND to classify each number.
Conclusion
In this article, we covered three effective methods to write a C program to check whether a number is even or odd. Each method—using the modulus operator, bitwise AND operator, and division-multiplication logic—has its advantages. Whether you’re a beginner or an experienced programmer, these techniques are essential for solving a wide range of computational problems.