QUESTION #021

Classify Triangle Type by Sides

Easy

📋 Problem Statement

Check whether a triangle is Equilateral, Isosceles, or Scalene based on its three sides.

📐 Triangle Classification by Sides

  • Equilateral Triangle: All three sides are equal (a = b = c)
  • Isosceles Triangle: Exactly two sides are equal (a = b OR b = c OR a = c)
  • Scalene Triangle: All three sides are different (a ≠ b ≠ c)
Equilateral
    /\
   /  \
5 /    \ 5
 /______\
     5

All sides equal

Isosceles
    /\
   /  \
7 /    \ 7
 /______\
     4

Two sides equal

Scalene
    /\
   /  \
8 /    \ 6
 /______\
     5

All sides different

📥 Input Format

Enter three integers as input

📤 Output Format

Print the output as "Equilateral" or "Isosceles" or "Scalene"

💡 Examples

Example 1:
Input: 60 52 60
Output: Isosceles
Analysis:
• a = 60, b = 52, c = 60
• Check: a = 60, c = 60 (two sides are equal)
• b = 52 (different from the other two)
Result: Exactly two sides equal → Isosceles Triangle
Example 2:
Input: 17 15 31
Output: Scalene
Analysis:
• a = 17, b = 15, c = 31
• Check: 17 ≠ 15, 15 ≠ 31, 17 ≠ 31
• All three sides are different
Result: All sides different → Scalene Triangle
Example 3:
Input: 25 25 25
Output: Equilateral
Analysis:
• a = 25, b = 25, c = 25
• Check: a = b, b = c, a = c
• All three sides are equal
Result: All sides equal → Equilateral Triangle

⚠️ Constraints

-10^9 <= INPUT <= 10^9

✅ Solution

#include <iostream>

using namespace std;

int main() {
    long long a, b, c;
    cin >> a >> b >> c;
    
    // Check if all three sides are equal (Equilateral)
    if (a == b && b == c)
        cout << "Equilateral";
    // Check if any two sides are equal (Isosceles)
    else if (a == b || b == c || c == a)
        cout << "Isosceles";
    // All sides are different (Scalene)
    else
        cout << "Scalene";
    
    return 0;
}

🔑 Key Concepts

  • Conditional Priority: Check Equilateral first (most specific), then Isosceles, then Scalene
  • AND Operator (&&): All conditions must be true for Equilateral
  • OR Operator (||): At least one condition must be true for Isosceles
  • Else Clause: Handles remaining case (Scalene) automatically
  • Mutually Exclusive Cases: A triangle can only be one type
  • Long Long Data Type: Handling values up to 10^9

💡 Understanding the Logic Flow

Step-by-Step Decision Process:

  • Step 1: Check if a == b && b == c
    • If true → All three sides equal → "Equilateral"
    • If false → Continue to next check
  • Step 2: Check if a == b || b == c || c == a
    • If true → At least two sides equal → "Isosceles"
    • If false → Continue to else
  • Step 3: If neither condition is true
    • All sides must be different → "Scalene"

Why Order Matters: Equilateral triangles also satisfy the Isosceles condition (a == b is true when all are equal), so we must check Equilateral first!

⚠️ Common Mistakes to Avoid

  • Wrong Order of Checks: Checking Isosceles before Equilateral will incorrectly classify equilateral triangles
    // ❌ WRONG: Equilateral (5,5,5) will output "Isosceles"
    if (a == b || b == c || c == a)  // Checked first!
        cout << "Isosceles";
    else if (a == b && b == c)
        cout << "Equilateral";
  • Using && instead of ||: a == b && b == c for Isosceles would miss cases like (5,5,3)
  • Incomplete Isosceles Check: Only checking a == b misses other equal pairs
  • Data Type Issues: Using int instead of long long for large constraints
  • Not handling all cases: Forgetting the else clause for Scalene

📊 Understanding the Conditions: Truth Table

Example    | a==b | b==c | a==c | a==b&&b==c | a==b||b==c||a==c | Result
-----------|------|------|------|------------|------------------|------------
(5,5,5)    |  T   |  T   |  T   |     T      |        T         | Equilateral
(5,5,3)    |  T   |  F   |  F   |     F      |        T         | Isosceles
(5,3,5)    |  F   |  F   |  T   |     F      |        T         | Isosceles
(3,5,5)    |  F   |  T   |  F   |     F      |        T         | Isosceles
(3,4,5)    |  F   |  F   |  F   |     F      |        F         | Scalene

Key Insight: Equilateral triangles make all equality checks true, while Isosceles makes at least one true.

🎓 Alternative Approach: Counting Equal Pairs

You can also solve this by counting how many pairs of sides are equal:

int equal_count = 0;
if (a == b) equal_count++;
if (b == c) equal_count++;
if (a == c) equal_count++;

if (equal_count == 3)
    cout << "Equilateral";  // All pairs equal
else if (equal_count >= 1)
    cout << "Isosceles";    // At least one pair equal
else
    cout << "Scalene";      // No pairs equal

Note: When all three sides are equal, all three pair comparisons (a==b, b==c, a==c) are true, giving count = 3.

🌍 Real-world Applications

  • Architecture: Identifying structural triangle types in roof trusses and bridges
  • Computer Graphics: Classifying triangular meshes for rendering optimization
  • Game Development: Collision detection and geometric pattern recognition
  • Engineering Design: Structural analysis and load distribution calculations
  • Mathematics Education: Automated geometry tutoring systems
  • CAD Software: Automatic shape recognition and validation
  • Pattern Recognition: Identifying geometric shapes in image processing
  • 3D Modeling: Optimizing mesh topology based on triangle types

🚀 Extension: Complete Triangle Analysis

Combine this with triangle validity checking for a complete solution:

long long a, b, c;
cin >> a >> b >> c;

// First, check if sides can form a valid triangle
if ((a + b > c) && (a + c > b) && (b + c > a)) {
    // Then classify the triangle type
    if (a == b && b == c)
        cout << "Valid Equilateral Triangle";
    else if (a == b || b == c || c == a)
        cout << "Valid Isosceles Triangle";
    else
        cout << "Valid Scalene Triangle";
} else {
    cout << "Not a valid triangle";
}

🎯 What You'll Learn

  • Working with mutually exclusive conditional branches
  • Understanding logical operator precedence (AND vs OR)
  • Importance of condition ordering in if-else chains
  • Pattern matching with equality comparisons
  • Geometric classification logic in programming
  • Writing clean, maintainable conditional code
  • Using else-if for multiple related conditions

💪 Practice Tips

  • Test Edge Cases: Try (0,0,0), negative numbers, very large numbers
  • Verify Logic: Draw a truth table to understand all possible combinations
  • Think About Order: What happens if you swap the if and else-if conditions?
  • Trace Through: Manually trace through examples (5,5,5), (5,5,3), (3,4,5)
  • Extend the Problem: Add angle-based classification (acute, right, obtuse)
  • Consider Input Validation: Should negative sides be allowed?