C++ Online Compiler
Example: Cube If Not Divisible by 2 and 3 in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// 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; }
Output
Clear
ADVERTISEMENTS