Q17 If the three sides of a triangle are entered through the keyboard, write a 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.

If the three sides of a triangle are entered through the keyboard, write a 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.

Program: 85

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.

#include<stdio.h>
#include<conio.h>
int main()
{
    //we are assuming only integer values for triangle sides
    int x, y, z;
    int max, valid;
    // x for AB y for BC z for AC

    //now calculate max side value among all the sides
    //Take input
    printf("Enter the first side of triangle: ");
    scanf("%d", &x);
    printf("Enter the second side of triangle: ");
    scanf("%d", &y);
    printf("Enter the third side of triangle: ");
    scanf("%d", &z);

    max = x;

    if(max<y)
        max=y;

    if(max<z)
        max=z;

    //now check for validation rules
    //if largest side is lower than the sum of two remaining sides
    if(max == x && x < (y+z))
        valid = 1;

    if(max == y && y < (x+z))
        valid = 1;

    if(max == z && z < (x+y))
        valid = 1;

if (valid==1)
    printf("Triangle is valid");
else
     x i' opiprintf("Triangle is not valid");
}

Output:

 Enter the first side of triangle: 2
 Enter the second side of triangle: 3
 Enter the third side of triangle: 4
 Triangle is valid

 Enter the first side of triangle: 4
 Enter the second side of triangle: 2
 Enter the third side of triangle: 1
 Triangle is not valid

Lokesh Kumar: Being EASTER SCIENCE's founder, Lokesh Kumar wants to share his knowledge and ideas. His motive is "We assist you to choose the best", He believes in different thinking.
Related Post
Leave a Comment

This website uses cookies.