Triangle is valid or not
C program to check whether the triangle is valid or not.
Validity of triangle can be checked in two ways:
A triangle is valid if sum of its two sides is greater than the third side. Means if a, b, c are three sides of a triangle. Then the triangle is valid if all three conditions are satisfied
a + b > c
a + c > b and
b + c > a
Program for the same is explained in Method 1 tab
A triangle is said to be a valid triangle if and only if sum of all the three angles is 180 degree and all the angles must be greater that 0.
Program for the same is explained in Method 2 tab
First way:
A triangle is valid if sum of its two sides is greater than the third side. Means if a, b, c are three sides of a triangle. Then the triangle is valid if all three conditions are satisfied
a + b > c
a + c > b and
b + c > a
Program for the same is explained in Method 1 tab
Second way:
A triangle is said to be a valid triangle if and only if sum of all the three angles is 180 degree and all the angles must be greater that 0.
Program for the same is explained in Method 2 tab
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int side1, side2, side3;
printf("Enter the length of three sides of the triangle\t:");
scanf("%d%d%d",&side1,&side2,&side3);
if((side1+side2>side3) && (side2+side3>side1) && (side1+side3>side2))
printf("\nTriangle is valid");
else
printf("\nTriangle is not valid");
getch();
}
Output
********** Run1 **********
Enter the length of three sides of the triangle :2
3
4
Triangle is valid
********** Run2 **********
Enter the length of three sides of the triangle :2
3
6
Triangle is not valid
Explanation
Let us take the three sides in the variables named say 'side1', 'side2' and 'side3'.
So, a triangle is valid if
otherwise, the triangle is not valid.
So, a triangle is valid if
(side1+side2>side3) && (side2+side3>side1) && (side1+side3>side2)
otherwise, the triangle is not valid.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int angle1, angle2, angle3, sum;
printf("Enter the three angles of the triangle\t:");
scanf("%d%d%d",&angle1,&angle2,&angle3);
sum = angle1 + angle2 + angle3;
if(sum==180 && angle1!=0 && angle2!=0 && angle3!=0)
printf("\nTriangle is valid");
else
printf("\nTriangle is not valid");
getch();
}
Output
********** Run1 **********
Enter the three angles of the triangle :30
60
40
Triangle is not valid
********** Run2 **********
Enter the three angles of the triangle :30
60
90
Triangle is valid
Explanation
All three angles of triangle are inputted in variables named 'angle1', 'angle2' and 'angle3'. Sum of all three angles is calculated and stored in the variable named 'sum'
Check the condition
In addition, make sure all the three angles are greater than 0 using the condition
sum = angle1 + angle2 + angle3
Check the condition
(sum == 180)
. If the condition is true, then, triangle can be formed otherwise not.
In addition, make sure all the three angles are greater than 0 using the condition
(angle1 != 0 && angle2 != 0 && angle3 != 0)
.