C++ Program To Determine Divisible Even And Odd Of 2 And 3
This article provides a clear guide on how to write a C++ program that determines if a given number is even, odd, and divisible by 2 or 3. You will learn to apply basic arithmetic operations and conditional logic to categorize numbers effectively.
Problem Statement
Accurately classifying numbers based on their divisibility and parity (even or odd) is a fundamental task in programming. This capability is essential for data validation, filtering, and various mathematical computations. The challenge is to create a robust C++ program that takes an integer input and reports whether it is even, odd, divisible by 2, and divisible by 3.
Example
Consider the number 6. Input: 6 Output: The number 6 is Even. The number 6 is divisible by 2. The number 6 is divisible by 3.
Consider the number 5. Input: 5 Output: The number 5 is Odd. The number 5 is not divisible by 2. The number 5 is not divisible by 3.
Background & Knowledge Prerequisites
To understand this program, readers should be familiar with:
- C++ Basic Syntax: How to write and structure a simple C++ program.
- Variables: Declaring and using integer variables (
int). - Input/Output Operations: Using
coutfor output andcinfor input. - Conditional Statements: Using
ifandelsefor decision-making. - Modulo Operator (
%): Understanding how to use it to find the remainder of a division. For example,number % 2 == 0checks if a number is even.
Use Cases or Case Studies
Understanding number properties like divisibility and parity has numerous practical applications:
- Data Validation: Ensuring user input meets specific criteria, such as requiring an even number for a certain field.
- Algorithm Optimization: In algorithms, certain operations might only apply to even or odd numbers, or numbers divisible by a specific factor.
- Number Games and Puzzles: Developing games that involve guessing or categorizing numbers based on their properties.
- Resource Allocation: Distributing items evenly or in groups of three.
- Financial Calculations: Identifying transactions that are multiples of certain denominations.
Solution Approaches
This section details a common and straightforward approach to determine the divisibility and parity of a number using C++.
Approach 1: Using the Modulo Operator and Conditional Statements
This approach directly applies the modulo operator (%) within if-else statements to check for even/odd and divisibility by 3.
One-line Summary Read an integer, then use the modulo operator to check its remainder when divided by 2 and 3, printing the corresponding properties.
Code Example
// Divisibility and Parity Checker
#include <iostream>
using namespace std;
int main() {
int number;
// Step 1: Prompt the user to enter a number
cout << "Enter an integer: ";
cin >> number;
// Step 2: Determine if the number is Even or Odd
if (number % 2 == 0) {
cout << "The number " << number << " is Even." << endl;
} else {
cout << "The number " << number << " is Odd." << endl;
}
// Step 3: Determine if the number is divisible by 2
// (This is inherently checked by the Even/Odd logic, but explicitly stated for clarity)
if (number % 2 == 0) {
cout << "The number " << number << " is divisible by 2." << endl;
} else {
cout << "The number " << number << " is not divisible by 2." << endl;
}
// Step 4: Determine if the number is divisible by 3
if (number % 3 == 0) {
cout << "The number " << number << " is divisible by 3." << endl;
} else {
cout << "The number " << number << " is not divisible by 3." << endl;
}
return 0;
}
Sample Output
Enter an integer: 10
The number 10 is Even.
The number 10 is divisible by 2.
The number 10 is not divisible by 3.
Enter an integer: 9
The number 9 is Odd.
The number 9 is not divisible by 2.
The number 9 is divisible by 3.
Enter an integer: 7
The number 7 is Odd.
The number 7 is not divisible by 2.
The number 7 is not divisible by 3.
Stepwise Explanation
- Include Header: The
#includeline brings in the input/output stream library, allowing the program to interact with the console.using namespace std;avoids repetitive typing ofstd::. mainFunction: This is the entry point of every C++ program.- Declare Variable: An
intvariable namednumberis declared to store the integer input from the user. - Get User Input:
-
cout << "Enter an integer: ";displays a prompt on the screen.
-
cin >> number; reads the integer entered by the user and stores it in the number variable.- Check Even/Odd:
-
if (number % 2 == 0): This condition uses the modulo operator. If the remainder ofnumberdivided by 2 is 0, the number is even.
-
else: If the condition is false (remainder is not 0), the number is odd, and "is Odd" is printed.- Check Divisibility by 2:
- Similar to the even/odd check,
if (number % 2 == 0)determines if the number is exactly divisible by 2. This check logically overlaps with the even check but is included separately for clarity as requested.
- Similar to the even/odd check,
- Check Divisibility by 3:
-
if (number % 3 == 0): This condition checks if the remainder ofnumberdivided by 3 is 0. If true, the number is divisible by 3.
-
- Return 0:
return 0;indicates that the program executed successfully.
Conclusion
This article demonstrated a simple yet effective C++ program to determine if a given integer is even, odd, and divisible by 2 or 3. By leveraging the modulo operator and conditional statements, programmers can easily implement logic for categorizing numbers based on fundamental arithmetic properties.
Summary
- The modulo operator (
%) is crucial for checking divisibility. - A number
Nis even ifN % 2 == 0; otherwise, it's odd. - A number
Nis divisible byXifN % X == 0. - Conditional
if-elsestatements are used to execute specific code blocks based on these divisibility checks. - These basic checks are foundational for many programming tasks involving numeric data.