QUESTION #022

Validate Triangle Using Angles

Easy

📋 Problem Statement

Input all angles of a triangle and check whether it is a Valid or Not Valid triangle.

📐 Triangle Angle Sum Property

For three angles to form a valid triangle, they must satisfy two conditions:

  • Condition 1: The sum of all three angles must equal exactly 180°
  • Condition 2: Each individual angle must be greater than 0°

Mathematical representation:

  • a + b + c = 180
  • a > 0 AND b > 0 AND c > 0

Both conditions must be true simultaneously.


/\
/ \
/ \
/ \
/________\
b° c°

Valid Triangle: a + b + c = 180° AND a > 0, b > 0, c > 0

📥 Input Format

Enter three integers as input

📤 Output Format

Print the output as "Valid" or "Not Valid"

💡 Examples

Example 1:
Input: 130 30 60
Output: Not Valid
Verification:
• a = 130°, b = 30°, c = 60°
• Check sum: 130 + 30 + 60 = 220° ✗
• Expected sum: 180°
• Check positive: All angles > 0 ✓
Result: Sum ≠ 180° → Not Valid
The angles add up to more than 180°, which violates the fundamental triangle property.
Example 2:
Input: 70 70 40
Output: Valid
Verification:
• a = 70°, b = 70°, c = 40°
• Check sum: 70 + 70 + 40 = 180° ✓
• Check positive: 70 > 0, 70 > 0, 40 > 0 ✓
Result: All conditions satisfied → Valid triangle
This forms an isosceles triangle with two equal angles of 70°.
Example 3:
Input: 60 60 60
Output: Valid
Verification:
• a = 60°, b = 60°, c = 60°
• Check sum: 60 + 60 + 60 = 180° ✓
• Check positive: All angles > 0 ✓
Result: Perfect equilateral triangle → Valid
All angles equal = equilateral triangle.
Example 4 (Edge Case):
Input: 0 90 90
Output: Not Valid
Verification:
• a = 0°, b = 90°, c = 90°
• Check sum: 0 + 90 + 90 = 180° ✓
• Check positive: a = 0 (NOT > 0) ✗
Result: One angle is 0° → Not Valid
Even though the sum is 180°, an angle of 0° creates a degenerate triangle (straight line).
Example 5 (Edge Case):
Input: -30 110 100
Output: Not Valid
Verification:
• a = -30°, b = 110°, c = 100°
• Check sum: -30 + 110 + 100 = 180° ✓
• Check positive: a = -30 (negative) ✗
Result: Negative angle → Not Valid
Negative angles don't exist in geometric triangles.

⚠️ Constraints

-10^9 <= Angles <= 10^9

✅ Solution

#include <iostream>

using namespace std;

int main() {
    int a, b, c;
    cin >> a >> b >> c;
    
    // Check both conditions: sum = 180 AND all angles positive
    if (a + b + c == 180 && a != 0 && b != 0 && c != 0)
        cout << "Valid";
    else
        cout << "Not Valid";
    
    return 0;
}

🔑 Key Concepts

  • Triangle Angle Sum Property: Fundamental geometric rule that angles sum to 180°
  • Compound AND Conditions: Multiple conditions must all be true simultaneously
  • Zero Check: Using != 0 to ensure positive angles (excludes negative and zero)
  • Edge Case Handling: Preventing degenerate triangles (0° or negative angles)
  • Single Condition Statement: Combining all checks into one if statement
  • Geometric Validation: Applying mathematical theorems in programming

💡 Understanding the Validation Logic

Why Check for Non-Zero?

Using a != 0 instead of a > 0 is intentional:

  • a != 0: Excludes both 0 and negative values
    • 0° → creates a degenerate triangle (flat line)
    • Negative angles → geometrically impossible
  • a > 0: More explicit "greater than zero" check
    • Both approaches work for this problem
    • != 0 is slightly more concise

Complete Validation:

// All four conditions combined with AND (&&)
if (
    a + b + c == 180  // Condition 1: Sum equals 180
    &&
    a != 0            // Condition 2: First angle valid
    &&
    b != 0            // Condition 3: Second angle valid
    &&
    c != 0            // Condition 4: Third angle valid
)

⚠️ Common Mistakes to Avoid

  • Only checking the sum: if (a + b + c == 180) would accept (0, 90, 90) as valid
    // ❌ WRONG: Missing positive checks
    if (a + b + c == 180)
        cout << "Valid";  // Accepts invalid cases!
  • Forgetting zero check: Zero angles create degenerate triangles (straight lines)
  • Not checking negative angles: Angles like -30° would be accepted if only checking sum
  • Using OR instead of AND: || would make the logic incorrect
  • Separate if statements: Not combining conditions can lead to wrong logic flow
  • Wrong equality check: Using = instead of == for comparison

🚀 Alternative Validation Approaches

Approach 1: Explicit Positive Check

if (a + b + c == 180 && a > 0 && b > 0 && c > 0)
    cout << "Valid";
else
    cout << "Not Valid";

Approach 2: Range Check (More Comprehensive)

// Check if each angle is in valid range (0, 180)
if (a + b + c == 180 && 
    a > 0 && a < 180 &&
    b > 0 && b < 180 &&
    c > 0 && c < 180)
    cout << "Valid";
else
    cout << "Not Valid";

Approach 3: Separate Validation Steps

bool sum_valid = (a + b + c == 180);
bool angles_positive = (a > 0 && b > 0 && c > 0);

if (sum_valid && angles_positive)
    cout << "Valid";
else
    cout << "Not Valid";

🎓 Extension: Triangle Classification by Angles

Once you know it's valid, you can classify the triangle by its angles:

  • Acute Triangle: All three angles < 90° (e.g., 60°, 60°, 60°)
  • Right Triangle: One angle = 90° (e.g., 90°, 45°, 45°)
  • Obtuse Triangle: One angle > 90° (e.g., 120°, 40°, 20°)
  • Equiangular Triangle: All angles equal = 60° each (special case of acute)
// Extended classification
if (a == 90 || b == 90 || c == 90)
    cout << "Right Triangle";
else if (a > 90 || b > 90 || c > 90)
    cout << "Obtuse Triangle";
else
    cout << "Acute Triangle";

🌍 Real-world Applications

  • Navigation Systems: GPS triangulation requires valid angle measurements
  • Computer Graphics: Rendering engines validate triangle meshes for 3D models
  • Surveying: Land measurement using theodolites and angle measurements
  • Architecture: Structural design validation for triangular supports
  • Robotics: Path planning and obstacle detection using triangulation
  • Game Development: Collision detection and physics calculations
  • Astronomy: Stellar parallax calculations using angular measurements
  • Engineering: Stress analysis in truss structures
  • Education Software: Automated geometry problem validation

📚 Mathematical Background

The Triangle Angle Sum Theorem

  • Euclidean Geometry: In flat (Euclidean) space, triangle angles always sum to exactly 180°
  • Historical Note: Known since ancient Greek mathematics (Euclid's Elements)
  • Proof Method: Can be proven using parallel lines and alternate interior angles
  • Universal Property: True for all triangles regardless of size or shape
  • Non-Euclidean Exception: In spherical geometry (curved surfaces), angles can sum to > 180°

Why Each Angle Must Be Positive:

  • Zero Angle: Creates a degenerate triangle (collapses to a line)
  • Negative Angle: Geometrically undefined in standard Euclidean geometry
  • Practical Limit: Each angle must be in range (0°, 180°) exclusive

🎯 What You'll Learn

  • Applying geometric theorems (angle sum property) in code
  • Writing compound conditional statements with multiple AND conditions
  • Understanding edge case validation (zero and negative values)
  • Distinguishing between valid and degenerate geometric shapes
  • Combining mathematical rules with logical operators
  • Input validation techniques for geometric problems
  • Clean, readable single-condition validation

💪 Challenge Yourself

Try solving these variations:

  • Level 1: Also output the type of triangle (Acute/Right/Obtuse)
  • Level 2: Check if angles form an equiangular triangle (all 60°)
  • Level 3: Given two angles, calculate and display the third angle
  • Level 4: Validate multiple triangles and count how many are valid
  • Level 5: Check if angles can form a valid triangle AND classify by both angles and hypothetical sides

Test Cases to Try:

  • Normal: (60, 60, 60), (90, 45, 45), (30, 70, 80)
  • Edge: (0, 90, 90), (180, 0, 0), (1, 1, 178)
  • Invalid: (100, 100, 100), (-30, 110, 100), (0, 0, 0)