Example
Input
Input first angle: 60 Input second angle: 30 Input third angle: 90
Output
The triangle is valid
Property of a triangle
A triangle is said to be a valid triangle if and only if sum of its angles is 180 °.
Logic to check triangle validity if angles are given
Step by step descriptive logic to check whether a triangle can be formed or not, if angles are given.
- Input all three angles of triangle in some variable say angle1, angle2 and angle3.
- Find sum of all three angles, store sum in some variable say
sum = angle1 + angle2 + angle3
. - Check
if(sum == 180)
then, triangle can be formed otherwise not. In addition, make sure angles are greater than 0 i.e. check condition for anglesif(angle1 > 0 && angle2 > 0 && angle3 > 0)
.
/**
* C program to check whether a triangle is valid or not if angles are given
*/
#include <stdio.h>
int main()
{
int angle1, angle2, angle3, sum;
/* Input all three angles of triangle */
printf("Enter three angles of triangle: \n");
scanf("%d%d%d", &angle1, &angle2, &angle3);
/* Calculate sum of angles */
sum = angle1 + angle2 + angle3;
/*
* If sum of angles is 180 and
* angle1, angle2, angle3 is not 0 then
* triangle is valid.
*/
if(sum == 180 && angle1 > 0 && angle2 > 0 && angle3 > 0)
{
printf("Triangle is valid.");
}
else
{
printf("Triangle is not valid.");
}
return 0;
}
/*
Output :
Enter three angles of triangle:
30
60
90
Triangle is valid.
*/