7. Check Positive, Negative or Zero

Easy ⏱️ 7 min

Problem Statement

Given an integer input, whether the given input is "Positive" or "Negative" or "Zero" and print the corresponding message

Input Format:
Enter an integer as a input
Output Format:
Print the output as "Negative" or "Positive" or "Zero"
Constraints:
1 <= INPUT <= 10^15
Sample Input 1:
-98
Sample Output 1:
NEGATIVE
Sample Input 2:
0
Sample Output 2:
ZERO
🧱 Three-Way Decision Making with Else-If Ladder
🔢 Understanding Number Classification

All numbers can be classified into exactly three categories:

1. Negative Numbers (< 0):

  • Any number less than zero
  • Examples: -1, -98, -5000, -1234567
  • Test: if (a < 0)

2. Positive Numbers (> 0):

  • Any number greater than zero
  • Examples: 1, 42, 5000, 9876543
  • Test: if (a > 0)

3. Zero (== 0):

  • Exactly zero, neither positive nor negative
  • Only one value: 0
  • Test: if (a == 0) or use else

✅ These three categories are mutually exclusive - a number can only belong to ONE category!

🪜 The Else-If Ladder Structure

Our three-way decision structure:

if (condition1) {
  // First option
} else if (condition2) {
  // Second option
} else {
  // Third option (default)
}

How it works:

  1. Check condition1 first
  2. If TRUE → Execute first block and exit
  3. If FALSE → Check condition2
  4. If condition2 is TRUE → Execute second block and exit
  5. If both are FALSE → Execute else block

💡 Key insight: Only ONE block executes, never more than one!

In our problem:

  • if (a < 0) → NEGATIVE
  • else if (a > 0) → POSITIVE
  • else → ZERO (the only remaining possibility)
🎯 Why Order Matters in Else-If

In this problem, you can check in any order since the conditions are mutually exclusive:

Option 1 (Our approach):

if (a < 0) NEGATIVE
else if (a > 0) POSITIVE
else ZERO

Option 2 (Also valid):

if (a > 0) POSITIVE
else if (a < 0) NEGATIVE
else ZERO

Option 3 (Also valid):

if (a == 0) ZERO
else if (a > 0) POSITIVE
else NEGATIVE

✅ All three produce the same result!

⚠️ When order DOES matter:

In problems with overlapping conditions, order is crucial:

// Grade classification example
if (marks >= 90) A
else if (marks >= 80) B
else if (marks >= 70) C

Here, 95 satisfies both >= 90 and >= 80, but only the first match executes!

🔍 Step-by-Step Code Execution

Example 1: a = -98

  1. Read input: a = -98
  2. Check: if (-98 < 0) → TRUE ✅
  3. Execute: cout << "NEGATIVE";
  4. Exit the ladder (skip remaining conditions)
  5. Output: NEGATIVE

Example 2: a = 0

  1. Read input: a = 0
  2. Check: if (0 < 0) → FALSE ❌
  3. Check: else if (0 > 0) → FALSE ❌
  4. Both conditions failed, go to else
  5. Execute: cout << "ZERO";
  6. Output: ZERO

Example 3: a = 42

  1. Read input: a = 42
  2. Check: if (42 < 0) → FALSE ❌
  3. Check: else if (42 > 0) → TRUE ✅
  4. Execute: cout << "POSITIVE";
  5. Exit the ladder (skip else block)
  6. Output: POSITIVE
💭 The Final Else: Default Case

The final else block acts as a catch-all for anything not matched above.

Why we don't need to check a == 0:

If we reach the else block, we already know:

  • a < 0 is FALSE (not negative)
  • a > 0 is FALSE (not positive)
  • Therefore, a must equal 0! ✅

This could be written explicitly:

else if (a == 0) {
  cout << "ZERO";
}

But it's redundant because there's no other possibility!

💡 Best practice: Use else for the "everything else" case when you've covered all other possibilities.

⚠️ When to be explicit: If there's a chance of unexpected values (like in error handling), explicitly check all conditions.

🎨 Alternative Approaches

Approach 1: Nested If-Else (Not recommended)

if (a == 0) {
  cout << "ZERO";
} else {
  if (a > 0) {
    cout << "POSITIVE";
  } else {
    cout << "NEGATIVE";
  }
}

❌ More complex, harder to read

Approach 2: Multiple Ifs (Wrong!)

if (a < 0) cout << "NEGATIVE";
if (a > 0) cout << "POSITIVE";
if (a == 0) cout << "ZERO";

❌ Checks all three conditions even after finding a match (inefficient)

Approach 3: Else-If Ladder (Our solution - Best!)

if (a < 0) cout << "NEGATIVE";
else if (a > 0) cout << "POSITIVE";
else cout << "ZERO";

✅ Clean, efficient, easy to understand

💡 Why else-if is best: It stops checking once a condition is met, making it both efficient and logical!

🌍 Real-World Applications

Where three-way classification is used:

  • 💰 Banking: Checking account balance (deficit, zero, surplus)
  • 🌡️ Temperature: Below freezing, freezing point, above freezing
  • 📊 Stock Market: Loss, break-even, profit
  • ⚖️ Comparisons: Less than, equal to, greater than
  • 🎮 Gaming: Health below threshold, exactly dead, alive
  • 📈 Analytics: Negative growth, no change, positive growth

Expanding to more categories:

The else-if ladder can handle any number of categories:

  • Grade systems (A, B, C, D, F)
  • Age groups (child, teen, adult, senior)
  • Priority levels (low, medium, high, critical)
  • Menu selections in programs

💡 This pattern is fundamental to decision-making in all programming!

💡 Tip: Use else-if ladders when you have mutually exclusive options - it's more efficient than multiple independent if statements!

Solution

#include <iostream>

using namespace std;

int main() {
    long long a;
    cin >> a;
    
    if (a < 0)
        cout << "NEGATIVE";
    else if (a > 0)
        cout << "POSITIVE";
    else
        cout << "ZERO";
    
    return 0;
}