This program reads the three sides of a triangle and determines whether it is equilateral, isosceles, scalene, or not a valid triangle at all — using nested if-else statements.
C Program
#include <stdio.h>
int main() {
float a, b, c;
printf("Enter the three sides of the triangle: ");
scanf("%f %f %f", &a, &b, &c);
if (a <= 0 || b <= 0 || c <= 0) {
printf("Not a valid triangle.\n");
}
else if ((a + b <= c) || (b + c <= a) || (a + c <= b)) {
printf("Not a valid triangle.\n");
}
else {
if (a == b && b == c) {
printf("Equilateral triangle.\n");
}
else if (a == b || b == c || a == c) {
printf("Isosceles triangle.\n");
}
else {
printf("Scalene triangle.\n");
}
}
return 0;
}
Explanation
- First, the program rejects non-positive side lengths.
- The triangle inequality theorem is checked: the sum of any two sides must be strictly greater than the third side. If this fails, it isn't a valid triangle.
- If valid, nested if-else checks:
- All three sides equal → Equilateral
- Exactly two sides equal → Isosceles
- All sides different → Scalene
Sample Output
Enter the three sides of the triangle: 5 5 5
Equilateral triangle.
Enter the three sides of the triangle: 1 2 10
Not a valid triangle.