Example
Input
Input first side: 7 Input second side: 10 Input third side: 5
Output
Triangle is valid
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 satisfieda + b > c
a + c > b
andb + c > a
Logic to check triangle validity
Step by step descriptive logic to check triangle validity if its sides are given.
- Input sides of a triangle from user. Store them in some variable say side1, side2 and side1.
- Given triangle is valid if
side1 + side2 > side3
andside1 + side3 > side2
andside2 + 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;
}
/*
Output :
Enter three sides of triangle: 7
4
10
Triangle is valid.
*/
Program to check valid triangle using nested if
/**
* 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;
}
/*
Output :
Enter three sides of triangle: 7
4
10
Triangle is valid.
*/
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.
*/