Write a C Program to Check Triangle is Valid or Not using Angles. Any triangle is valid if the sum of the three angles in a triangle is equal to 180
C Program to Check Triangle is Valid or Not using Angles Example
This C program helps the user to enter all angles of a triangle. And then check whether it is valid or not using If Else.
#include<stdio.h>
int main()
{
int angle1, angle2, angle3, Sum;
printf("\n Please Enter Three Angles of a Triangle : ");
scanf("%d%d%d", &angle1, &angle2, &angle3);
Sum = angle1 + angle2 + angle3;
if(Sum == 180)
{
printf("\n This is a Valid Triangle");
}
else
{
printf("\n This is an Invalid Triangle");
}
return 0;
}
Please Enter Three Angles of a Triangle : 45 75 60
This is a Valid Triangle
Let me try another value
Please Enter Three Angles of a Triangle : 60 50 90
This is an Invalid Triangle
Check Triangle is Valid or Not using Angles Example 2
In the above program, we forgot to check whether any of the angles is zero or not. It is also important while validating a triangle. So, we used a Logical AND operator in C Programming to make sure all angles are greater than 0
#include<stdio.h>
int main()
{
int angle1, angle2, angle3, Sum;
printf("\n Please Enter Three Angles of a Triangle : ");
scanf("%d%d%d", &angle1, &angle2, &angle3);
Sum = angle1 + angle2 + angle3;
if(Sum == 180 && angle1 != 0 && angle2 != 0 && angle3 != 0)
{
printf("\n This is a Valid Traingle");
}
else
{
printf("\n This is an Invalid Triangle");
}
return 0;
}
Now let me pass the correct angles
Please Enter Three Angles of a Triangle : 70 70 40
This is a Valid Triangle