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

Previous Program Next Program

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

Check triangle validity if sides are given
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 and c are three sides of a triangle. The triangle is valid if all three conditions are satisfied - a + b > c, a + c > b and b + c > a.

After knowing the basic property of triangle. Let us concentrate to the logic section. Below is the step by step descriptive logic -

  1. Read all three sides of the triangle. Store them in some variable say a, b and c.
  2. 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;
} 
Output
Enter three sides of triangle: 7
4
10
Triangle is valid.

Happy coding ;)

You may also like

Previous Program Next Program
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
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

No comments:

Post a Comment