c triangle check if triangle is 90 degrees
Checking if a Triangle is 90 Degrees in C
To check if a triangle is a right-angled triangle (90 degrees), you can use the Pythagorean theorem. The theorem states that in a right-angled triangle, the square of the length of the hypotenuse is equal to the sum of the squares of the lengths of the other two sides.
Here's a step-by-step explanation of how to check if a triangle is a right-angled triangle in C:
- Input the sides of the triangle:
Input the lengths of the three sides of the triangle.
Calculate the squares of the sides:
Calculate the squares of the lengths of the three sides.
Identify the longest side as the hypotenuse:
Identify the longest side as the hypotenuse (the side opposite the right angle in a right-angled triangle).
Apply the Pythagorean theorem:
Check if the square of the length of the hypotenuse is equal to the sum of the squares of the lengths of the other two sides.
Output the result:
- If the condition is satisfied, then the triangle is a right-angled triangle. Otherwise, it is not.
Here's a simple example of how this can be implemented in C:
#include <stdio.h>
#include <math.h>
int main() {
float side1, side2, side3;
printf("Enter the lengths of the three sides of the triangle: ");
scanf("%f %f %f", &side1, &side2, &side3);
// Identify the longest side as the hypotenuse
float hypotenuse = (side1 > side2) ? ((side1 > side3) ? side1 : side3) : ((side2 > side3) ? side2 : side3);
// Apply the Pythagorean theorem
if (hypotenuse hypotenuse == (side1 side1 + side2 side2 + side3 side3 - hypotenuse * hypotenuse)) {
printf("The triangle is a right-angled triangle.\n");
} else {
printf("The triangle is not a right-angled triangle.\n");
}
return 0;
}
This code takes the lengths of the three sides of the triangle as input and then checks if it forms a right-angled triangle using the Pythagorean theorem.