C Program to Check Triangle Validity
One way to implement the input of triangular sides judgement is through the following methods:
- First, use the scanf function to input the lengths of the three sides of the triangle separately, assuming they are a, b, and c.
- Then, use an if statement to check the lengths of the triangle’s sides.
- Firstly, verify whether the three sides satisfy the triangle inequality theorem, which states that a + b > c, a + c > b, and b + c > a. If this condition is not met, then it indicates that a triangle cannot be formed.
- Next, determine the type of triangle. If all three sides are equal, it is an equilateral triangle; if only two sides are equal, it is an isosceles triangle; if all three sides are different lengths, it is a scalene triangle.
- In the end, output the corresponding information based on the judgment result.
Here is a complete example code:
#include <stdio.h>
int main() {
float a, b, c;
printf("请输入三角形的三边长:");
scanf("%f %f %f", &a, &b, &c);
if (a + b > c && a + c > b && b + c > a) {
if (a == b && b == c) {
printf("这是一个等边三角形。\n");
} else if (a == b || a == c || b == c) {
printf("这是一个等腰三角形。\n");
} else {
printf("这是一个普通三角形。\n");
}
} else {
printf("这三条边无法构成三角形。\n");
}
return 0;
}
Please note that in the code above, we assume that the user is inputting float type for the length of the sides. If you need to input integer type for the length of the sides, you can simply change float to int.