C Program To Check Whether Given Number Is Even Or Odd
Determining whether a number is even or odd is a fundamental concept in programming, crucial for various logical operations. In this article, you will learn how to implement C programs to efficiently check the parity of a given integer.
Problem Statement
An even number is an integer that is exactly divisible by 2, leaving no remainder. An odd number, conversely, is an integer that is not exactly divisible by 2, meaning it leaves a remainder of 1 when divided by 2. The challenge is to write a C program that can reliably classify any given integer as either even or odd, which is essential for data validation, algorithmic design, and problem-solving.
Example
Let's consider two examples to understand the desired output for even and odd numbers:
- Input: 7 - Output: 7 is an odd number.
- Input: 10 - Output: 10 is an even number.
Background & Knowledge Prerequisites
To understand the solutions presented in this article, you should have a basic understanding of:
- C Variables: Declaring and using integer variables.
- Input/Output Operations: Using
printf()for output andscanf()for input. - Conditional Statements:
ifandelsestatements for decision-making. - Arithmetic Operators: Specifically, the modulo operator (
%). - Bitwise Operators: The bitwise AND operator (
&) for advanced solutions.
Use Cases or Case Studies
Checking if a number is even or odd has several practical applications across different domains:
- Data Validation: Ensuring that user input meets specific criteria, such as requiring an even number for a certain ID.
- Game Development: Alternating player turns or game states (e.g., player 1 acts on even rounds, player 2 on odd rounds).
- Algorithm Design: Optimizing loops or calculations by performing different actions based on number parity.
- Array Processing: Distributing elements into two groups (even-indexed vs. odd-indexed elements) or processing even/odd numbers differently.
- Cryptography: Some cryptographic algorithms involve operations that differentiate between even and odd numbers.
Solution Approaches
Here are two common and efficient approaches to determine if a number is even or odd in C.
Approach 1: Using the Modulo Operator (%)
This approach leverages the modulo operator, which returns the remainder of a division. For even numbers, division by 2 yields a remainder of 0. For odd numbers, the remainder is 1.
// Check Even or Odd Using Modulo
#include <stdio.h>
int main() {
int num;
// Step 1: Prompt the user to enter a number
printf("Enter an integer: ");
// Step 2: Read the integer input from the user
scanf("%d", &num);
// Step 3: Check if the number is even or odd using the modulo operator
if (num % 2 == 0) {
printf("%d is an even number.\\n", num);
} else {
printf("%d is an odd number.\\n", num);
}
return 0;
}
Sample Output:
Enter an integer: 15
15 is an odd number.
Enter an integer: 20
20 is an even number.
Stepwise Explanation:
- Include Header: The
stdio.hheader is included for standard input/output functions likeprintfandscanf. - Declare Variable: An integer variable
numis declared to store the user's input. - Get Input: The program prompts the user to enter an integer using
printfand then reads the input usingscanf, storing it innum. - Check Parity: The
ifstatement checks the conditionnum % 2 == 0.
- If the remainder of
numdivided by 2 is 0, the number is even, and the corresponding message is printed. - Otherwise (if the remainder is not 0, meaning it's 1 for positive integers), the number is odd, and the
elseblock executes, printing the odd message.
- Return 0:
mainfunction returns 0, indicating successful execution.
Approach 2: Using the Bitwise AND Operator (&)
This method uses the bitwise AND operator. The least significant bit (LSB) of an even number is always 0, while the LSB of an odd number is always 1. Performing a bitwise AND operation with 1 (& 1) effectively isolates the LSB.
// Check Even or Odd Using Bitwise AND
#include <stdio.h>
int main() {
int num;
// Step 1: Prompt the user to enter a number
printf("Enter an integer: ");
// Step 2: Read the integer input from the user
scanf("%d", &num);
// Step 3: Check if the number is even or odd using the bitwise AND operator
// If the LSB is 0, num & 1 will be 0 (even)
// If the LSB is 1, num & 1 will be 1 (odd)
if ((num & 1) == 0) {
printf("%d is an even number.\\n", num);
} else {
printf("%d is an odd number.\\n", num);
}
return 0;
}
Sample Output:
Enter an integer: 12
12 is an even number.
Enter an integer: 9
9 is an odd number.
Stepwise Explanation:
- Include Header:
stdio.his included for input/output functions. - Declare Variable: An integer variable
numis declared. - Get Input: The program prompts for and reads an integer from the user.
- Check Parity: The
ifstatement checks the condition(num & 1) == 0.
- Bitwise AND with 1: When
numis bitwise ANDed with1, the result is0ifnumis even (its binary representation ends with...0). - The result is
1ifnumis odd (its binary representation ends with...1). - If
(num & 1)evaluates to 0, the number is even. - Otherwise, the number is odd.
- Return 0:
mainfunction returns 0.
Conclusion
Determining if a number is even or odd is a basic yet crucial programming task. Both the modulo operator (%) and the bitwise AND operator (&) provide efficient ways to achieve this in C. The modulo operator is generally more intuitive and readable for beginners, while the bitwise AND operator offers a performance advantage in some contexts due to operating directly on binary representations.
Summary
- An even number is perfectly divisible by 2 (remainder 0).
- An odd number leaves a remainder of 1 when divided by 2.
- Modulo Operator (
%): Thenum % 2 == 0condition is the most straightforward way to check for even numbers. - Bitwise AND Operator (
&): The(num & 1) == 0condition checks the least significant bit (LSB) to determine parity, often being slightly faster. - Both methods are reliable for classifying integers as even or odd in C programs.