Triangle Valid or Not based On Sides: C Program


If the three sides of a Triangle are entered through the keyboard, write a C program to check whether the Triangle is valid or not. The Triangle is valid if the sum of two sides is greater than the largest of the three sides.

Related Read:
Basic Arithmetic Operations In C
Find Area of a Triangle Using Its Sides: C Program
Triangle Valid or Not based On Angles: C Program

Logic To Find Valid Triangle or Not

First we find out biggest side in the 3 sides of the triangle. Next we add the other 2 sides. Now the addition of the other 2 sides must be greater than the biggest side of the Triangle, for a Triangle to be valid. If not, its not a Triangle.

Example:
If a, b and c are 3 sides of the Triangle. If c is the largest side. Then for the Triangle to be valid, (a+b) must be greater than c.
(a+b) > c
If this is true, then Triangle is valid, if not, its invalid Triangle.

Triangle Valid or Not based On Sides: C Program


[youtube https://www.youtube.com/watch?v=XMJdWL_FDck]

YouTube Link: https://www.youtube.com/watch?v=XMJdWL_FDck [Watch the Video In Full Screen.]


Triangle Valid or Not based On Sides: C Program

#include < stdio.h >

int main()
{
    float a, b, c, flag = 0;

    printf("Enter values for a, b and c\n");
    scanf("%f%f%f", &a, &b, &c);

    if( a>b && a>c )
    {
        flag = ((b+c) > a);
    }
    else if( b>c )
    {
        flag = ((a+c) > b);
    }
    else
    {
        flag = ((a+b) > c);
    }

    if(flag)
    {
        printf("Valid Triangle\n");
    }
    else
    {
        printf("Invalid Triangle\n");
    }

    return 0;
}

Output 1:
Enter values for a, b and c
10
4
3
Invalid Triangle

Output 2:
Enter values for a, b and c
10
15
6
Valid Triangle

In above source code, variable flag stores true(1) or false(0) value.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *