Input all angles of a triangle and check whether it is a Valid or Not Valid triangle.
For three angles to form a valid triangle, they must satisfy two conditions:
Mathematical representation:
a + b + c = 180
a > 0 AND b > 0 AND c > 0
Both conditions must be true simultaneously.
Valid Triangle: a + b + c = 180° AND a > 0, b > 0, c > 0
Enter three integers as input
Print the output as "Valid" or "Not Valid"
-10^9 <= Angles <= 10^9
#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; }
!= 0
to ensure positive angles (excludes negative and zero)Why Check for Non-Zero?
Using a != 0
instead of a > 0
is intentional:
a != 0
: Excludes both 0 and negative values
a > 0
: More explicit "greater than zero" check
!= 0
is slightly more conciseComplete 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 )
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!
||
would make the logic incorrect=
instead of ==
for comparisonApproach 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";
Once you know it's valid, you can classify the triangle by its angles:
// 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";
The Triangle Angle Sum Theorem
Why Each Angle Must Be Positive:
Try solving these variations:
Test Cases to Try: