C Program To Find The Cube Of A Number If Not Divisor Of 2 And 3
In this article, you will learn how to write a C program that calculates the cube of a number only if it meets a specific condition: it must not be divisible by both 2 and 3. This guide will walk you through the logic, prerequisites, and a practical C implementation.
Problem Statement
Often in programming, we need to perform operations based on specific criteria. A common requirement is to check a number's divisibility before proceeding with further calculations. The challenge here is to compute the cube of an integer, but *only* if that integer is not divisible by 2 AND not divisible by 3. If the number fails this condition (i.e., it is divisible by 2, or by 3, or by both), the program should indicate that the condition is not met, rather than calculating the cube.
Example
Consider the number 7.
- 7 is not divisible by 2 (7 % 2 = 1).
- 7 is not divisible by 3 (7 % 3 = 1).
- Since both conditions are true, its cube (7 * 7 * 7 = 343) would be calculated and displayed.
Now consider the number 6.
- 6 is divisible by 2 (6 % 2 = 0).
- 6 is divisible by 3 (6 % 3 = 0).
- Since it is divisible by 2 (and 3), the condition is not met, and the program should output a message indicating this.
Background & Knowledge Prerequisites
To understand and implement the solution, a basic understanding of C programming concepts is helpful:
- Variables: How to declare and use integer variables (
int). - Input/Output: Using
printf()for output andscanf()for input. - Arithmetic Operators: Specifically, the multiplication operator (
*). - Modulo Operator: The
%operator, which gives the remainder of a division. This is crucial for checking divisibility. - Conditional Statements: The
if-elsestatement for executing code blocks based on conditions. - Logical Operators: The logical AND operator (
&&) to combine multiple conditions.
No special libraries beyond the standard input/output library (stdio.h) are required.
Use Cases or Case Studies
This type of conditional logic for number processing is useful in various scenarios:
- Data Validation: Filtering data based on specific numerical properties before processing. For example, in a system processing unique identifiers, you might only process IDs that don't match certain problematic patterns (like those divisible by 2 or 3, which might indicate a test ID).
- Game Development: In some game mechanics, certain power-ups or actions might only be available if a player's score or level number meets a complex set of criteria.
- Cryptography/Hashing: While simplified, basic number theory and conditional checks are foundational. Some steps might only proceed if an intermediate value is not a multiple of specific primes.
- Educational Tools: Creating programs that help students explore number properties and conditional logic in a tangible way.
- Resource Allocation: Allocating specific resources or permissions only to users whose numerical IDs satisfy particular non-divisibility rules to ensure fair or specific distribution.
Solution Approaches
The most straightforward and efficient approach for this problem involves using the modulo operator within an if statement to check the divisibility conditions, followed by calculating the cube.
Approach 1: Conditional Cube Calculation with Modulo Operator
This approach directly implements the problem's criteria using C's if-else statement and the modulo operator.
Summary: Read an integer, check if it's not divisible by 2 AND not divisible by 3 using the modulo operator and logical AND (&&). If true, calculate and print its cube; otherwise, print a message indicating the condition was not met.
// Cube of a Number (Conditional)
#include <stdio.h>
int main() {
int number;
long long cube; // Use long long for cube to handle larger results
// Step 1: Prompt the user to enter an integer
printf("Enter an integer: ");
// Step 2: Read the integer from the user
scanf("%d", &number);
// Step 3: Check if the number is NOT divisible by 2 AND NOT divisible by 3
if ((number % 2 != 0) && (number % 3 != 0)) {
// Step 4: If the condition is met, calculate the cube
cube = (long long)number * number * number;
printf("The number %d is not divisible by 2 and not divisible by 3.\\n", number);
printf("The cube of %d is: %lld\\n", number, cube);
} else {
// Step 5: If the condition is not met, print an appropriate message
printf("The number %d is divisible by 2 or 3 (or both). Cannot compute its cube under the given condition.\\n", number);
}
return 0;
}
Sample Output:
Case 1: Number meets the condition (e.g., 7)
Enter an integer: 7
The number 7 is not divisible by 2 and not divisible by 3.
The cube of 7 is: 343
Case 2: Number does not meet the condition (e.g., 6)
Enter an integer: 6
The number 6 is divisible by 2 or 3 (or both). Cannot compute its cube under the given condition.
Case 3: Number does not meet the condition (e.g., 5, which is not divisible by 2 but IS divisible by 3? No, 5 is not divisible by 3. Let's try 4.)
Enter an integer: 4
The number 4 is divisible by 2 or 3 (or both). Cannot compute its cube under the given condition.
Case 4: Number does not meet the condition (e.g., 9)
Enter an integer: 9
The number 9 is divisible by 2 or 3 (or both). Cannot compute its cube under the given condition.
Stepwise Explanation:
- Include Header: The
#includeline brings in the standard input/output library, necessary forprintfandscanf. - Declare Variables:
-
int number;declares an integer variable to store the user's input. -
long long cube;declares along longvariable to store the cube. This data type is used because the cube of anintcan exceed the maximum value anintcan hold, preventing overflow for larger inputs.
- Get Input:
-
printf("Enter an integer: ");displays a prompt for the user. -
scanf("%d", &number);reads the integer entered by the user and stores it in thenumbervariable.
- Conditional Check:
-
if ((number % 2 != 0) && (number % 3 != 0))is the core of the logic. -
number % 2 != 0: Checks if the remainder whennumberis divided by 2 is not 0. This meansnumberis not divisible by 2. -
number % 3 != 0: Checks if the remainder whennumberis divided by 3 is not 0. This meansnumberis not divisible by 3. -
&&(logical AND): This operator ensures that *both* conditions must be true for the entire expression to be true.
- Calculate and Print Cube (if condition met):
- If the
ifcondition is true,cube = (long long)number * number * number;calculates the cube. The(long long)cast ensures that the multiplication is performed usinglong longarithmetic, preventing intermediate overflows. -
printf()statements then display a confirmation and the calculated cube.
- Handle Condition Not Met (else block):
- If the
ifcondition is false (meaning the number is divisible by 2, or by 3, or both), the code inside theelseblock executes. - A
printf()message informs the user why the cube was not computed.
- Return 0:
return 0;indicates successful program execution.
Conclusion
This article demonstrated how to implement a conditional cube calculation in C. By leveraging the modulo operator and if-else statements, you can create robust programs that perform specific operations only when certain criteria are met. This fundamental concept of conditional logic is a cornerstone of programming, allowing for flexible and intelligent decision-making within your code.
Summary
- The program calculates the cube of an integer.
- The calculation is performed *only* if the number is not divisible by 2 AND not divisible by 3.
- The modulo operator (
%) is used to check for divisibility (e.g.,number % 2 != 0). - The logical AND operator (
&&) combines the two non-divisibility conditions. - An
if-elsestatement controls whether the cube is calculated or a message is displayed. - Using
long longfor the cube variable prevents potential integer overflow for larger input numbers.