Given an integer value, if it is divisible by 3 print "HI", if it is divisible by 5 print "HELLO". If it is divisible by both print "HIHELLO", else print "NONE"
The &&
(AND) operator combines two conditions - BOTH must be true for the whole expression to be true.
Basic Syntax:
if (condition1 && condition2) {
// Executes only if BOTH are true
}
Truth Table for AND:
Condition 1 | Condition 2 | Result (&&) |
---|---|---|
TRUE | TRUE | ✅ TRUE |
TRUE | FALSE | ❌ FALSE |
FALSE | TRUE | ❌ FALSE |
FALSE | FALSE | ❌ FALSE |
In our problem:
a % 3 == 0 && a % 5 != 0
→ Divisible by 3 AND NOT by 5 → "HI"a % 5 == 0 && a % 3 != 0
→ Divisible by 5 AND NOT by 3 → "HELLO"a % 3 == 0 && a % 5 == 0
→ Divisible by BOTH 3 AND 5 → "HIHELLO"💡 Short-circuit evaluation: If condition1 is FALSE, condition2 is never checked! The computer knows the result must be FALSE.
⚠️ THE PROBLEM: Numbers divisible by both 3 AND 5 exist!
Example: a = 15
❌ WRONG APPROACH (without exclusivity):
if (a % 3 == 0) cout << "HI";
else if (a % 5 == 0) cout << "HELLO";
else if (a % 3 == 0 && a % 5 == 0) cout << "HIHELLO";
What happens with a = 15?
15 % 3 == 0
→ TRUE ✅Result: Prints "HI" instead of "HIHELLO" → WRONG!
✅ CORRECT APPROACH (with exclusivity):
if (a % 3 == 0 && a % 5 != 0) cout << "HI";
else if (a % 5 == 0 && a % 3 != 0) cout << "HELLO";
else if (a % 3 == 0 && a % 5 == 0) cout << "HIHELLO";
What happens with a = 15 now?
15 % 3 == 0 && 15 % 5 != 0
→ TRUE && FALSE → FALSE ❌15 % 5 == 0 && 15 % 3 != 0
→ TRUE && FALSE → FALSE ❌15 % 3 == 0 && 15 % 5 == 0
→ TRUE && TRUE → TRUE ✅💡 Key Insight: The != 0
conditions make categories mutually exclusive - a number can only match ONE condition!
Even with exclusivity conditions, we must check the "BOTH" case first or make it exclusive!
Our solution order:
✅ This works! The exclusivity (!= 0
) ensures each category is separate.
Alternative valid order:
if (a % 3 == 0 && a % 5 == 0) cout << "HIHELLO";
else if (a % 3 == 0) cout << "HI";
else if (a % 5 == 0) cout << "HELLO";
else cout << "NONE";
✅ This also works! By checking "BOTH" first, we don't need exclusivity on the simple conditions.
Why our approach is better:
💡 Best practice: Make conditions mutually exclusive for maximum clarity and correctness!
Example 1: a = 55 (divisible by 5 only)
55 % 3 == 0 && 55 % 5 != 0
55 % 5 == 0 && 55 % 3 != 0
Example 2: a = 12 (divisible by 3 only)
12 % 3 == 0 && 12 % 5 != 0
Example 3: a = 15 (divisible by BOTH)
15 % 3 == 0 && 15 % 5 != 0
15 % 5 == 0 && 15 % 3 != 0
15 % 3 == 0 && 15 % 5 == 0
Example 4: a = 7 (divisible by neither)
7 % 3 == 0 && 7 % 5 != 0
7 % 5 == 0 && 7 % 3 != 0
7 % 3 == 0 && 7 % 5 == 0
else
Condition 1: a % 3 == 0 && a % 5 != 0
a % 3 == 0
→ "Is a divisible by 3?"a % 5 != 0
→ "Is a NOT divisible by 5?"Condition 2: a % 5 == 0 && a % 3 != 0
a % 5 == 0
→ "Is a divisible by 5?"a % 3 != 0
→ "Is a NOT divisible by 3?"Condition 3: a % 3 == 0 && a % 5 == 0
a % 3 == 0
→ "Is a divisible by 3?"a % 5 == 0
→ "Is a divisible by 5?"💡 Math insight: Numbers divisible by both 3 and 5 are divisible by 15 (Least Common Multiple)!
So a % 3 == 0 && a % 5 == 0
is equivalent to a % 15 == 0
Comprehensive test cases to verify correctness:
Input | % 3 | % 5 | Category | Output |
---|---|---|---|---|
3 | 0 ✅ | 3 ❌ | Only 3 | HI |
5 | 2 ❌ | 0 ✅ | Only 5 | HELLO |
7 | 1 ❌ | 2 ❌ | Neither | NONE |
12 | 0 ✅ | 2 ❌ | Only 3 | HI |
15 | 0 ✅ | 0 ✅ | Both | HIHELLO |
20 | 2 ❌ | 0 ✅ | Only 5 | HELLO |
30 | 0 ✅ | 0 ✅ | Both | HIHELLO |
55 | 1 ❌ | 0 ✅ | Only 5 | HELLO |
✅ All test cases pass with our mutually exclusive conditions!
Where AND operator is used in real programming:
1. Access Control & Authentication:
if (username == "admin" && password == "secure123") {
// Grant access - BOTH must match
}
2. Range Checking:
if (age >= 18 && age <= 65) {
// Eligible for standard insurance
}
3. Form Validation:
if (email.contains("@") && password.length() >= 8) {
// Valid registration
}
4. Game Logic:
if (hasKey && doorIsLocked) {
// Unlock the door - need BOTH conditions
}
5. Business Rules:
if (orderTotal >= 100 && isMember) {
// Apply 20% discount
}
6. Safety Checks:
if (engineOn && fuelLevel > 0) {
// Car can drive
}
💡 Key principle: When multiple conditions MUST all be satisfied, use AND (&&)!
Three main logical operators in C++:
1. AND Operator (&&):
a > 0 && a < 100
→ Between 1 and 992. OR Operator (||):
a == 0 || a == 1
→ Either 0 or 13. NOT Operator (!):
!(a == 0)
→ Same as a != 0
Combining operators:
if ((a % 3 == 0 || a % 5 == 0) && a != 15) {
// Divisible by 3 OR 5, but NOT 15
}
💡 Use parentheses to make complex conditions clear!
💡 Critical Takeaway: When categories can overlap (like divisibility by multiple numbers), use exclusivity conditions (!= 0
) to ensure each category is separate and only ONE can match!
#include <iostream>
using namespace std;
int main() {
long long a;
cin >> a;
if (a % 3 == 0 && a % 5 != 0)
cout << "HI";
else if (a % 5 == 0 && a % 3 != 0)
cout << "HELLO";
else if (a % 3 == 0 && a % 5 == 0)
cout << "HIHELLO";
else
cout << "NONE";
return 0;
}