C++ Program To Find The Cube Of A Number If Not Divisor Of 2 And 3
This article will guide you through creating a C++ program that calculates the cube of a number only if it is not divisible by both 2 and 3. You will learn about conditional logic and basic arithmetic operations in C++.
Problem Statement
In programming, it is often necessary to perform an action only when specific conditions are met. This problem requires us to accept an integer input and then determine if it satisfies a dual condition: it must not be divisible by 2 AND it must not be divisible by 3. If both conditions are true, we proceed to calculate and display the number's cube; otherwise, we inform the user that the condition was not met. This type of conditional execution is fundamental for building robust applications that respond dynamically to user input or data states.
Example
Consider the number 5.
- 5 is not divisible by 2 (
5 % 2 != 0). - 5 is not divisible by 3 (
5 % 3 != 0).
5 * 5 * 5 = 125.
Background & Knowledge Prerequisites
To understand and implement the solution, readers should be familiar with:
- Basic C++ syntax: variable declaration, data types (e.g.,
int). - Input/Output operations:
std::coutfor printing andstd::cinfor reading input. - Arithmetic operators: multiplication (
*) and modulus (%). - Conditional statements:
ifandelse. - Logical operators: logical AND (
&&).
No special libraries or complex setups are required beyond a standard C++ compiler.
Use Cases or Case Studies
Conditional logic based on divisibility finds applications in various scenarios:
- Data Validation: Ensuring user input meets specific criteria (e.g., a number must be odd and not a multiple of three for a specific calculation).
- Game Development: Implementing game rules where actions are triggered only if certain conditions are met (e.g., a power-up applies if the player's score is not divisible by 10 or 7).
- Number Theory Algorithms: Filtering numbers based on their properties before applying further mathematical operations.
- Resource Allocation: Distributing resources only to tasks whose identifiers meet specific divisibility rules to balance loads.
- Filtered Reporting: Generating reports for items with specific IDs that are not multiples of certain numbers, helping to highlight specific patterns.
Solution Approaches
We will implement a direct approach using an if statement to check the combined divisibility conditions.
Simple Conditional Check
This approach uses a single if statement with the logical AND operator (&&) to verify both divisibility conditions simultaneously.
// Cube If Not Divisible by 2 and 3
#include <iostream>
int main() {
// Step 1: Declare a variable to store the user's input number
int number;
// Step 2: Prompt the user to enter a number
std::cout << "Enter an integer: ";
// Step 3: Read the number from the user
std::cin >> number;
// Step 4: Check if the number is NOT divisible by 2 AND NOT divisible by 3
if ((number % 2 != 0) && (number % 3 != 0)) {
// Step 5: If the condition is true, calculate its cube
long long cube = static_cast<long long>(number) * number * number; // Use long long for larger cubes
// Step 6: Display the cube
std::cout << "The number " << number << " is not divisible by 2 and not divisible by 3.\\n";
std::cout << "Its cube is: " << cube << std::endl;
} else {
// Step 7: If the condition is false, inform the user
std::cout << "The number " << number << " is divisible by 2 or 3 (or both), so its cube is not calculated.\\n";
}
return 0;
}
Sample Output
Here are examples of running the program with different inputs:
Input: 5
Enter an integer: 5
The number 5 is not divisible by 2 and not divisible by 3.
Its cube is: 125
Input: 6
Enter an integer: 6
The number 6 is divisible by 2 or 3 (or both), so its cube is not calculated.
Input: 9
Enter an integer: 9
The number 9 is divisible by 2 or 3 (or both), so its cube is not calculated.
Input: 10
Enter an integer: 10
The number 10 is divisible by 2 or 3 (or both), so its cube is not calculated.
Stepwise Explanation
- Include Header:
#includeis used to allow input and output operations (likestd::coutandstd::cin). mainFunction: The program's execution begins here.- Declare Variable: An integer variable
numberis declared to store the value entered by the user. - Prompt and Input:
std::coutdisplays a message asking the user to enter a number, andstd::cinreads that number into thenumbervariable. - Conditional Check:
-
number % 2 != 0: This checks if the remainder whennumberis divided by 2 is not 0. If true, the number is odd.
-
number % 3 != 0: This checks if the remainder when number is divided by 3 is not 0. If true, the number is not a multiple of 3.&& (Logical AND): This operator ensures that *both* conditions (number is odd AND number is not a multiple of 3) must be true for the if block to execute.- Calculate Cube: If the conditions are met,
number * number * numbercalculates the cube.static_castensures that the product doesn't overflow(number) intifnumberis large, aslong longcan hold a larger range of values. - Display Result: The program prints the original number and its calculated cube.
elseBlock: If the conditions in theifstatement are not met (meaning the number is divisible by 2, or by 3, or both), theelseblock executes, informing the user that the cube was not calculated.- Return 0: Indicates that the program executed successfully.
Conclusion
This article demonstrated how to implement a C++ program that conditionally calculates the cube of a number. By leveraging basic arithmetic operators (%, *) and conditional statements (if, else) combined with logical operators (&&), we can create flexible programs that perform actions only when specific criteria are met. This fundamental concept is a cornerstone of programming logic, enabling applications to adapt to diverse inputs and requirements.
Summary
- Conditional Logic: Used
ifandelsestatements to control program flow based on specific conditions. - Divisibility Check: Employed the modulus operator (
%) to determine if a number is divisible by another. - Logical AND (
&&): Combined multiple conditions, requiring all of them to be true for the main action to proceed. - Cube Calculation: Performed basic arithmetic (
*) to find the cube of a number. - Data Type Consideration: Used
long longfor the cube to prevent potential integer overflow for larger input numbers.