If else Exercises
C program to check whether triangle is valid or not if sides are given
Write a C program to input side of a triangle and check whether triangle is valid or not using if else. How to check whether a triangle can be formed or not if sides of triangle is given using if else in C programming. 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, Relational operators, Nested If else
Property of triangle

A triangle is valid if sum of its two sides is greater than the third side. Means if a, b, c are three sides of a triangle. Then the triangle is valid if all three conditions are satisfied
a + b > c
a + c > b and
b + c > a
Logic to check triangle validity
Step by step descriptive logic to check triangle validity if its sides are given.
1.Input sides of a triangle from user. Store them in some variable say side1, side2 and side1.
2.Given triangle is valid if side1 + side2 > side3 and side1 + side3 > side2 and side2 + side3 > side1.
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 side1, side2, side3;
/* Input three sides of a triangle */
printf("Enter three sides of triangle: \n");
scanf("%d%d%d", &side1, &side2, &side3);
if((side1 + side2) > side3)
{
if((side2 + side3) > side1)
{
if((side1 + side3) > side2)
{
/*
* If side1 + side2 > side3 and
* side2 + side3 > side1 and
* side1 + side3 > side2 then
* the triangle 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;
}
Logic to check triangle validity using nested if - best approach
Despite of easiness, the above code is messy and less readable. In above code printf("Triangle is not valid."); statement is unnecessarily repeated for various conditions. You can cut the extra printf("Triangle is not valid."); statement using a flag variable.
Let us suppose a temporary variable valid initialized with 0 indicating triangle is not valid. The main idea is to check triangle validity conditions and if triangle is valid then set valid variable to 1 indicating triangle is valid. Finally, check if(valid == 1) then triangle is valid otherwise not valid.
Program to check valid triangle using nested if - best approach
/**
* C program to check whether a triangle is valid using nested if
*/
#include <stdio.h>
int main()
{
int side1, side2, side3;
/* Initially assume that the triangle is not valid */
int valid = 0;
/* Input all three sides of a triangle */
printf("Enter three sides of triangle: \n");
scanf("%d%d%d", &side1, &side2, &side3);
if((side1 + side2) > side3)
{
if((side2 + side3) > side1)
{
if((side1 + side3) > side2)
{
/*
* If side1 + side2 > side3 and
* side2 + side3 > side1 and
* side1 + side3 > side2 then
* the triangle is valid. Hence set
* valid variable to 1.
*/
valid = 1;
}
}
}
/* Check valid flag variable */
if(valid == 1)
{
printf("Triangle is valid.");
}
else
{
printf("Triangle is not valid.");
}
return 0;
}
Another way to cut length of the program is by using logical AND operator &&. Below program illustrates how to use logical AND operator for this program.
Program to check valid triangle using if...else and logical AND operator
/**
* C program to check whether a triangle is valid or not using logical AND operator
*/
#include <stdio.h>
int main()
{
int side1, side2, side3;
/* Input all three sides of a triangle */
printf("Enter three sides of triangle: \n");
scanf("%d%d%d", &side1, &side2, &side3);
if((side1 + side2 > side3) && (side1 + side3 > side2) && (side2 + side3 > side1))
{
printf("Triangle is valid.");
}
else
{
printf("Triangle is not valid.");
}
return 0;
}
Output
Enter three sides of triangle: 7
4
10
Triangle is valid.
