8. HI, HELLO or HIHELLO

Easy ⏱️ 10 min

Problem Statement

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"

Input Format:
Enter an integer as a input
Output Format:
Follow the format as sample output - "HI"or "HELLO" or "HIHELLO" or "NONE"
Constraints:
1 <= INPUT <= 10^15
Sample Input 1:
55
Sample Output 1:
HELLO
Sample Input 2:
12
Sample Output 2:
HI
🔗 Compound Conditions with AND Operator (&&)
⚡ The AND Operator (&&) - Both Must Be True

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.

🎯 Why We Need Exclusivity Conditions (Critical!)

⚠️ THE PROBLEM: Numbers divisible by both 3 AND 5 exist!

Example: a = 15

  • 15 % 3 == 0 ✅ (divisible by 3)
  • 15 % 5 == 0 ✅ (divisible by 5)

❌ 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?

  1. Check: 15 % 3 == 0 → TRUE ✅
  2. Print "HI" and EXIT the ladder
  3. Never reaches the "HIHELLO" condition! 😱

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?

  1. Check: 15 % 3 == 0 && 15 % 5 != 0 → TRUE && FALSE → FALSE ❌
  2. Check: 15 % 5 == 0 && 15 % 3 != 0 → TRUE && FALSE → FALSE ❌
  3. Check: 15 % 3 == 0 && 15 % 5 == 0 → TRUE && TRUE → TRUE ✅
  4. Print "HIHELLO" → CORRECT!

💡 Key Insight: The != 0 conditions make categories mutually exclusive - a number can only match ONE condition!

🧩 Why Order Still Matters Here

Even with exclusivity conditions, we must check the "BOTH" case first or make it exclusive!

Our solution order:

  1. Divisible by 3 BUT NOT 5 → "HI"
  2. Divisible by 5 BUT NOT 3 → "HELLO"
  3. Divisible by BOTH → "HIHELLO"
  4. Neither → "NONE"

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:

  • 🎯 Explicit and clear: Each condition says exactly what it means
  • 🔒 Mutually exclusive: Impossible to match multiple conditions
  • 🐛 Less error-prone: No risk of forgetting to check "BOTH" first
  • 📖 Self-documenting: Code reads like the requirements

💡 Best practice: Make conditions mutually exclusive for maximum clarity and correctness!

🔍 Step-by-Step Execution Examples

Example 1: a = 55 (divisible by 5 only)

  1. Check: 55 % 3 == 0 && 55 % 5 != 0
    • 55 % 3 = 1 (not 0) → FALSE
    • FALSE && anything → FALSE ❌
  2. Check: 55 % 5 == 0 && 55 % 3 != 0
    • 55 % 5 = 0 → TRUE ✅
    • 55 % 3 = 1 (not 0) → TRUE ✅
    • TRUE && TRUE → TRUE ✅
  3. Print "HELLO" and exit
  4. Output: HELLO

Example 2: a = 12 (divisible by 3 only)

  1. Check: 12 % 3 == 0 && 12 % 5 != 0
    • 12 % 3 = 0 → TRUE ✅
    • 12 % 5 = 2 (not 0) → TRUE ✅
    • TRUE && TRUE → TRUE ✅
  2. Print "HI" and exit
  3. Output: HI

Example 3: a = 15 (divisible by BOTH)

  1. Check: 15 % 3 == 0 && 15 % 5 != 0
    • 15 % 3 = 0 → TRUE ✅
    • 15 % 5 = 0 (IS 0!) → FALSE ❌
    • TRUE && FALSE → FALSE ❌
  2. Check: 15 % 5 == 0 && 15 % 3 != 0
    • 15 % 5 = 0 → TRUE ✅
    • 15 % 3 = 0 (IS 0!) → FALSE ❌
    • TRUE && FALSE → FALSE ❌
  3. Check: 15 % 3 == 0 && 15 % 5 == 0
    • 15 % 3 = 0 → TRUE ✅
    • 15 % 5 = 0 → TRUE ✅
    • TRUE && TRUE → TRUE ✅
  4. Print "HIHELLO" and exit
  5. Output: HIHELLO

Example 4: a = 7 (divisible by neither)

  1. Check: 7 % 3 == 0 && 7 % 5 != 0
    • 7 % 3 = 1 (not 0) → FALSE ❌
    • FALSE && anything → FALSE ❌
  2. Check: 7 % 5 == 0 && 7 % 3 != 0
    • 7 % 5 = 2 (not 0) → FALSE ❌
    • FALSE && anything → FALSE ❌
  3. Check: 7 % 3 == 0 && 7 % 5 == 0
    • 7 % 3 = 1 (not 0) → FALSE ❌
    • FALSE && anything → FALSE ❌
  4. All conditions failed, execute else
  5. Print "NONE"
  6. Output: NONE
🧮 Complete Condition Breakdown

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?"
  • Combined → "Divisible by 3 but NOT by 5"
  • Examples: 3, 6, 9, 12, 18, 21, 24, 27, 33...
  • Excludes: 15, 30, 45, 60... (divisible by both)

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?"
  • Combined → "Divisible by 5 but NOT by 3"
  • Examples: 5, 10, 20, 25, 35, 40, 50, 55...
  • Excludes: 15, 30, 45, 60... (divisible by both)

Condition 3: a % 3 == 0 && a % 5 == 0

  • a % 3 == 0 → "Is a divisible by 3?"
  • a % 5 == 0 → "Is a divisible by 5?"
  • Combined → "Divisible by BOTH 3 AND 5"
  • Examples: 15, 30, 45, 60, 75, 90...
  • Pattern: All multiples of 15 (LCM of 3 and 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

📊 Testing All Possible Cases

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!

🌍 Real-World Applications of Compound 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 (&&)!

⚡ Logical Operators Comparison

Three main logical operators in C++:

1. AND Operator (&&):

  • Meaning: "Both conditions must be true"
  • Example: a > 0 && a < 100 → Between 1 and 99
  • Use when: ALL conditions are required

2. OR Operator (||):

  • Meaning: "At least one condition must be true"
  • Example: a == 0 || a == 1 → Either 0 or 1
  • Use when: ANY condition is sufficient

3. NOT Operator (!):

  • Meaning: "Reverse the condition"
  • Example: !(a == 0) → Same as a != 0
  • Use when: You need the opposite of a condition

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!

Solution

#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;
}