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

Write a C program to enter three sides of a 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 all sides of the triangle is given using if else in C programming. Program to check valid triangle if sides are known in C.

Example:
Input first side: 7
Input second side: 10
Input third side: 5
Output: Triangle is valid.

Required knowledge

Basic C programming, If else

Logic to check triangle can be formed or not

We have already seen how to check valid triangle if angles are known. Here we will be checking if its sides are given. We know that a triangle is a valid triangle if and only if sum of its two sides is greater than the third side. Suppose a, b, c are three side of a triangle, then the triangle can only be formed if
a + b > c and b + c > a and a + c > b
Read more about properties of triangle.

Program to check valid triangle

/**
 * 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.\n");
            }
            else
            {
                printf("Triangle is not valid.\n");
            }
        }
        else
        {
            printf("Triangle is not valid.\n");
        }
    }
    else
    {
        printf("Triangle is not valid.\n");
    }

    return 0;
} 


Note: You can also short the above program using below approach.


Program to check valid triangle

/** 
 * 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  
    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) //If a+b>c and a+c>b and b+c>a then it is valid  
            {  
                valid = 1;
            }  
        }
    }  
    
    if(valid == 1)
    {
        printf("Triangle is valid.\n");
    }
    else
    {
        printf("Triangle is not valid.\n");
    }
  
    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 short the logic.


Program to check valid triangle using logical AND operator

/**
 * 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.\n");
    }
    else
    {
        printf("Triangle is not valid.\n");
    }

    return 0;
} 


Output
Enter three sides of triangle: 7
4
10
Triangle is valid.

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment