C program to check whether triangle is valid or not if sides are given

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

Property of triangle

Validity of triangle with side

We call a triangle as a valid triangle if and only if; sum of its two sides is greater than the third side. Which 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

Below is the step by step descriptive logic to check triangle validity if its sides are given.

  1. Input all three sides of the triangle. Store them in some variable say side1, side2 and side1.
  2. Check the triangle validity condition. If side1 + side2 > side3 and side1 + side3 > side2 and side2 + side3 > side1 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 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) 
            {
                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 side1, side2, side3; 

    /*
     * Initially I have assumed 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)
            {  
                // 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;  
}

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

/**
 * 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 a+b > c and a+c > b and b+c > a then triangle is valid */
    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.

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>