- Write a C program to enter three sides of a triangle and check whether a triangle is valid or not.
Required Knowledge
- C printf and scanf functions
- If Else statement in C
- Relational Operators in C
A triangle is a valid triangle, If and only If, the sum of any two sides of a triangle is greater than the third side.
For Example, let A, B and C are three sides of a triangle. Then, A + B > C, B + C > A and C + A > B.
C program to check whether a triangle is valid, given sides of triangle
/* * Given three sides of a triangle, Write a c program to * check whether it is a valid triangle or not */ #include <stdio.h> int main() { int side1, side2, side3; /* * Take length of three sides of triangle as input * from user using scanf */ printf("Enter Length of Three Sides of a Triangle\n"); scanf("%d %d %d", &side1, &side2, &side3); if((side1 + side2 > side3)&&(side2 + side3 > side1) &&(side3 + side1 > side2)) { printf("It is a Valid Triangle\n"); } else { printf("It is an invalid Triangle"); } return 0; }
Output
Enter Length of Three Sides of a Triangle 10 20 20 It is a Valid Triangle
Enter Length of Three Sides of a Triangle 10 20 40 It is an invalid Triangle