Write a C program to enter sides of triangle and check whether the triangle is valid or not using if else. How to check whether a triangle can be formed or not if sides of the triangle is given using if else in C programming. Program to check valid triangle if sides are known in C. Logic to check triangle validity if sides are given in C program.
Example
Input
Input first side: 7 Input second side: 10 Input third side: 5
Output
Triangle is valid
Required knowledge
Basic C programming, If else, Basic Mathematics
Logic to check triangle validity if sides are given
As I stated earlier for this problem prerequisite is basic mathematics. We must know basic properties of triangle to check triangle validity if sides are known. Let us first learn some basic property of triangle.
Property of triangle
After knowing the basic property of triangle. Let us concentrate to the logic section. Below is the step by step descriptive logic -
- Read all three sides of the triangle. Store them in some variable say a, b and c.
- Check the triangle validity condition. If a + b > c and a + c > b and b + c > a. Then only the triangle is valid, otherwise not.
Program to check triangle validity using nested if else
/** * C program to check whether a triangle is valid or not if its sides are given */ #include <stdio.h> int main() { int a, b, c; //a, b, c are three sides of a triangle /* Reads all three sides of a triangle */ printf("Enter three sides of triangle: \n"); scanf("%d%d%d", &a, &b, &c); if((a+b) > c ) { if((b+c) > a) { if((a+c) > b) { //If a+b>c and a+c>b and b+c>a then it is valid printf("Triangle is valid."); } else { printf("Triangle is not valid."); } } else { printf("Triangle is not valid."); } } else { printf("Triangle is not valid."); } return 0; }
You might have observed that the printf("Triangle is not valid.") statement is repeating for various cases. You can cut down using a flag variable. Below program explains the rest concept.
Program to check valid triangle using nested if
/** * C program to check whether a triangle is valid */ #include <stdio.h> int main() { int a, b, c; //a, b, c are three sides of a triangle /* * Initially I have assumed that the triangle is not valid */ int valid = 0; /* Reads all three sides of a triangle */ printf("Enter three sides of triangle: \n"); scanf("%d%d%d", &a, &b, &c); if((a+b)>c ) { if((b+c) > a) { if((a+c) > b) { // Change the valid status of triangle to valid valid = 1; } } } if(valid == 1) { printf("Triangle is valid."); } else { printf("Triangle is not valid."); } return 0; }
Note: Another way to short the program is by using logical AND && operator. Below program illustrates how to use logical AND operator to cut down code even more.
Program to check valid triangle using if else
/** * C program to check whether a triangle is valid or not using logical AND operator */ #include <stdio.h> int main() { int a, b, c; //a, b, c are three sides of a triangle /* Reads all three sides of a triangle */ printf("Enter three sides of triangle: \n"); scanf("%d%d%d", &a, &b, &c); /* If a+b > c and a+c > b and b+c > a then triangle is valid */ if((a+b > c) && (a+c > b) && (b+c > a)) { printf("Triangle is valid."); } else { printf("Triangle is not valid."); } return 0; }
Enter three sides of triangle: 7 4 10 Triangle is valid.
Happy coding ;)
You may also like
- If else programming exercises index.
- C program to check whether a triangle is Equilateral, Isosceles or Scalene triangle.
- C program to enter two angles of a triangle and find the third angle.
- C program to find the area of a triangle.
- C program to find area of an Equilateral triangle.
- C program to enter any number and check whether it is negative, positive or zero.
- C program to check whether a number is even or odd.