Right Angle Triangle
Check if three given side lengths form a right-angled triangle using the Pythagorean theorem
Understand the Problem
Problem Statement
Given the lengths of three sides of a triangle (A, B, C), determine if the triangle is a right-angled triangle. A triangle is right-angled if the square of the longest side equals the sum of squares of the other two sides (Pythagorean theorem: a² + b² = c²).
Constraints
- 1 ≤ A, B, C ≤ 9999
- All inputs are positive integers representing side lengths
- The triangle inequality must be satisfied for a valid triangle
Examples
5
3
4YESThe sides are 3, 4, and 5. Sorting them gives 3, 4, 5. Checking: 3² + 4² = 9 + 16 = 25 = 5². Since 3² + 4² = 5², this forms a right-angled triangle.
11
6
9NOThe sides are 6, 9, and 11. Sorting them gives 6, 9, 11. Checking: 6² + 9² = 36 + 81 = 117, but 11² = 121. Since 6² + 9² ≠ 11², this does not form a right-angled triangle.
Solution
#include <stdio.h>
#include <math.h>
int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
// Sort the sides to identify the largest
if (a > b) {
int temp = a;
a = b;
b = temp;
}
if (b > c) {
int temp = b;
b = c;
c = temp;
}
if (a > b) {
int temp = a;
a = b;
b = temp;
}
// Check Pythagorean theorem: a² + b² = c²
if (a*a + b*b == c*c) {
printf("YES\n");
} else {
printf("NO\n");
}
return 0;
}The C solution follows these steps:
- Read three integers representing the side lengths
- Sort the three values using simple swap operations to identify the longest side (c) <3>Check if a² + b² equals c² using integer arithmetic
- Print "YES" if the Pythagorean theorem holds, otherwise "NO"
The sorting ensures we always compare the two shorter sides against the longest side.