Write a C program to check whether triangle is valid or not if angles are given using if else. How to check whether a triangle can be formed or not, if its angles are given using if else in C programming. Program to check a valid triangle based on its angles in C. Logic to check triangle validity if angles are given in C program.
Example
Input
Input first angle: 60 Input second angle: 30 Input third angle: 90
Output
The triangle is valid
Required knowledge
Basic C programming, If else, Basic Mathematics
Logic to check triangle validity if angles are given
As I mentioned it above solving this program requires basic mathematics. You must be aware of basic properties of triangle before attempting this program. Let us first learn basic properties of triangle.
Property of a triangle
A triangle is said to be a valid triangle if and only if sum of its angles is 180 °.
After knowing the basic property of triangle let us move on to the logic of this program. Below is the step by step descriptive logic to check whether a triangle can be formed or not, if angles are given.
- Read all three angles of the triangle in some variable say a, b and c.
- Find sum of all three angles, store the sum in some variable say sum = a + b + c.
- Check if sum == 180. If sum == 180, then make sure angles are greater than 0, then the triangle can be formed otherwise not.
Program to check triangle validity when angles are given
/** * C program to check whether a triangle is valid or not if angles are given */ #include <stdio.h> int main() { int a, b, c, sum; //a, b, c are three angles of a triangle /* Read all three angles of triangle */ printf("Enter three angles of triangle: \n"); scanf("%d%d%d", &a, &b, &c); /* Calculate sum of angles */ sum = a + b + c; /* Check the triangle validity property */ if(sum == 180 && a!=0 && b!=0 && c!=0) { printf("Triangle is valid."); } else { printf("Triangle is not valid."); } return 0; }
Enter three angles of triangle: 30 60 90 Triangle is valid.
Happy coding ;)
You may also like
- If else programming exercises index.
- C program to check triangle validity if sides are given.
- C program to check Equilateral, Isosceles or Scalene triangle.
- C program to find third angle of the triangle.
- C program to find area of a triangle.
- C program to calculate area of an Equilateral triangle.
- C program to check even or odd.
- C program to check leap year.