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
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
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| /** * 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; } |
Output
Enter three angles of triangle: 30 60 90 Triangle is valid.
No comments:
Post a Comment