Checking whether a number is even or odd is a fundamental task in programming. This article will explore various C Program to Check Whether a Number is Even or Odd. We will cover multiple approaches with examples, explanations, and outputs.
Prerequisites
Before diving into the examples, you should be familiar with:
- Basic C syntax and structure.
- Understanding of modulus operator
%
. - Functions in C.
Understanding the Problem
Even and Odd Numbers
- Even numbers are integers divisible by 2 without a remainder (e.g., -4, 0, 2, 8).
- Odd numbers are integers that have a remainder of 1 when divided by 2 (e.g., -3, 1, 7, 11).
Example 1: Using the Modulus Operator
1.1 Explanation
The simplest way to check if a number is even or odd is by using the modulus operator %
. If num % 2
equals 0, the number is even; otherwise, it’s odd.
1.2 Program: Modulus Operator
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("%d is even.\n", num);
else
printf("%d is odd.\n", num);
return 0;
}
1.3 Output
Enter an integer: 7
7 is odd.
Example 2: Using Bitwise AND Operator
2.1 Explanation
We can use the bitwise AND operator to check if a number is even or odd. In binary representation, even numbers have the least significant bit (LSB) as 0, and odd numbers have the LSB as 1. If num & 1
is 0, the number is even; otherwise, it’s odd.
2.2 Program: Bitwise AND Operator
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num & 1)
printf("%d is odd.\n", num);
else
printf("%d is even.\n", num);
return 0;
}
2.3 Output
Enter an integer: 8
8 is even.
Example 3: Using a Function
3.1 Explanation
We can create a function to encapsulate the logic of checking whether a number is even or odd. This makes the code more modular and reusable.
3.2 Program: Using a Function
#include <stdio.h>
void checkEvenOdd(int num);
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
checkEvenOdd(num);
return 0;
}
void checkEvenOdd(int num) {
if (num % 2 == 0)
printf("%d is even.\n", num);
else
printf("%d is odd.\n", num);
}
3.3 Output
Enter an integer: 5
5 is odd.
Example 4: Using a Ternary Operator
4.1 Explanation
The ternary operator can be used to check if a number is even or odd in a concise way. It evaluates a condition and returns one of two values based on the result.
4.2 Program: Ternary Operator
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
(num % 2 == 0) ? printf("%d is even.\n", num) : printf("%d is odd.\n", num);
return 0;
}
4.3 Output
Enter an integer: 10
10 is even.
Example 5: Using Recursion
5.1 Explanation
Although less common, we can use recursion to determine if a number is even or odd by subtracting 2 repeatedly until we reach 0 or 1.
5.2 Program: Recursion
#include <stdio.h>
int isEven(int num);
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (isEven(num))
printf("%d is even.\n", num);
else
printf("%d is odd.\n", num);
return 0;
}
int isEven(int num) {
if (num == 0)
return 1;
else if (num == 1)
return 0;
else
return isEven(num - 2);
}
5.3 Output
Enter an integer: 13
13 is odd.
Example 6: Using the abs
Function for Negative Numbers
6.1 Explanation
To handle negative numbers correctly, we can use the abs
function from the standard library to get the absolute value before checking if it is even or odd.
6.2 Program: abs
Function
#include <stdio.h>
#include <stdlib.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (abs(num) % 2 == 0)
printf("%d is even.\n", num);
else
printf("%d is odd.\n", num);
return 0;
}
6.3 Output
Enter an integer: -4
-4 is even.
Conclusion
This article covered various C Program to Check Whether a Number is Even or Odd. We explored using the modulus operator, bitwise operations, functions, ternary operator, recursion, and the abs
function for handling negative numbers. Each method has its unique advantages, and understanding these different approaches allows you to choose the most appropriate solution for your specific needs.
By mastering these techniques, you can apply similar logic to other programming challenges and enhance your problem-solving skills in C