C++ Program To Check Whether Number Is Even Or Odd
This article explores how to determine if a given number is even or odd using C++. You will learn the fundamental logic behind this check and practical C++ implementations.
Problem Statement
A number is considered even if it is perfectly divisible by 2, leaving no remainder. Conversely, a number is odd if it is not perfectly divisible by 2, meaning it leaves a remainder of 1 when divided by 2. Accurately identifying even or odd numbers is a basic but crucial operation in various programming scenarios.
Example
Let's quickly see how an even/odd check works:
Input: 7
Output: 7 is an Odd number.
Input: 10
Output: 10 is an Even number.
Background & Knowledge Prerequisites
To understand the solutions presented, you should have a basic grasp of:
- C++ syntax: How to write simple C++ programs.
- Input/Output operations: Using
coutfor output andcinfor input. - Conditional statements:
if-elsestructures. - Arithmetic operators: Specifically, the modulo operator (
%). - Bitwise operators: The bitwise AND operator (
&) for an alternative approach.
Use Cases or Case Studies
Checking if a number is even or odd has numerous practical applications:
- Array and List Processing: Alternate styling for elements (e.g., zebra striping in tables), processing elements at even/odd indices.
- Game Development: Determining turns (Player 1 on odd turns, Player 2 on even turns), movement patterns, or specific game logic based on numerical parity.
- Data Validation: Ensuring specific input adheres to an even or odd requirement.
- Mathematical Algorithms: Many number theory problems and algorithms rely on parity checks.
- User Interface (UI) Design: Highlighting rows in a list differently for better readability (e.g., even rows have a light grey background).
- Resource Allocation: Distributing tasks evenly or handling specific resources based on whether a task ID is even or odd.
Solution Approaches
We will explore two common and effective methods to check for even or odd numbers in C++.
Approach 1: Using the Modulo Operator (%)
The most straightforward way to check for even or odd is by using the modulo operator, which returns the remainder of a division.
Summary: A number n is even if n % 2 equals 0; otherwise, it's odd.
// Check Even or Odd using Modulo Operator
#include <iostream>
int main() {
int number;
// Step 1: Prompt the user to enter a number
std::cout << "Enter an integer: ";
std::cin >> number;
// Step 2: Use the modulo operator to check for even or odd
// If the remainder when divided by 2 is 0, it's even.
// Otherwise, it's odd.
if (number % 2 == 0) {
std::cout << number << " is an Even number." << std::endl;
} else {
std::cout << number << " is an Odd number." << std::endl;
}
return 0;
}
Sample Output:
Enter an integer: 42
42 is an Even number.
Enter an integer: 17
17 is an Odd number.
Stepwise Explanation:
- Include iostream: This line brings in the necessary library for input and output operations.
- Declare Variable: An integer variable
numberis declared to store the user's input. - Get Input: The program prompts the user to enter an integer and reads it into the
numbervariable usingstd::cin. - Apply Modulo Operator: The core logic
number % 2 == 0is evaluated.-
number % 2calculates the remainder whennumberis divided by 2.
-
0, the number is even.- Conditional Output:
- If the condition
(number % 2 == 0)is true, theifblock executes, printing that the number is even.
- If the condition
else block executes, printing that the number is odd.Approach 2: Using the Bitwise AND Operator (&)
A more performance-optimized approach, especially for embedded systems or low-level programming, involves using the bitwise AND operator. This method checks the Least Significant Bit (LSB) of the number.
Summary: In binary representation, an even number always has its LSB as 0, while an odd number always has its LSB as 1. Performing number & 1 isolates the LSB.
// Check Even or Odd using Bitwise AND Operator
#include <iostream>
int main() {
int number;
// Step 1: Prompt the user to enter a number
std::cout << "Enter an integer: ";
std::cin >> number;
// Step 2: Use the bitwise AND operator to check the LSB
// If (number & 1) is 0, the LSB is 0, meaning it's even.
// If (number & 1) is 1, the LSB is 1, meaning it's odd.
if ((number & 1) == 0) {
std::cout << number << " is an Even number." << std::endl;
} else {
std::cout << number << " is an Odd number." << std::endl;
}
return 0;
}
Sample Output:
Enter an integer: 8
8 is an Even number.
Enter an integer: 13
13 is an Odd number.
Stepwise Explanation:
- Include iostream: Standard input/output library.
- Declare Variable: An integer variable
numberis declared for input. - Get Input: The program takes an integer input from the user.
- Apply Bitwise AND: The key operation is
(number & 1).- Any integer
Ncan be represented in binary.1in binary is...0001.
- Any integer
N & 1, every bit of N is ANDed with 0 except for the LSB, which is ANDed with 1.N.0 (meaning N & 1 results in 0), the number is even.1 (meaning N & 1 results in 1), the number is odd.- Conditional Output:
- If
(number & 1) == 0is true, the number is even.
- If
Conclusion
Determining whether a number is even or odd is a foundational concept in programming. Both the modulo operator (%) and the bitwise AND operator (&) provide simple and effective solutions in C++. While the modulo operator is generally more intuitive and readable for beginners, the bitwise AND approach offers a slightly more efficient alternative for specific use cases.
Summary
- An even number is perfectly divisible by 2 (remainder 0).
- An odd number is not perfectly divisible by 2 (remainder 1).
- Modulo Operator (
%):number % 2 == 0for even,number % 2 != 0for odd. This is the most common and readable method. - Bitwise AND Operator (
&):(number & 1) == 0for even,(number & 1) != 0(or== 1) for odd. This method checks the Least Significant Bit (LSB) and can be slightly more performant. - Both methods provide reliable ways to classify numbers by parity.