C Program to Check Whether a Triangle is Valid or Not given Sides of Triangle

  • Write a C program to enter three sides of a triangle and check whether a triangle is valid or not.

Required Knowledge

A triangle is a valid triangle, If and only If, the sum of any two sides of a triangle is greater than the third side.
For Example, let A, B and C are three sides of a triangle. Then, A + B > C, B + C > A and C + A > B.


C program to check whether a triangle is valid, given sides of triangle

/*
 * Given three sides of a triangle, Write a c program to 
 * check whether it is a valid triangle or not 
 */  
  
#include <stdio.h>  
  
int main() {  
    int side1, side2, side3;  
  
    /* 
     * Take length of three sides of triangle as input 
     * from user using scanf 
     */  
    printf("Enter Length of Three Sides of a Triangle\n");  
    scanf("%d %d %d", &side1, &side2, &side3);     
  
    if((side1 + side2 > side3)&&(side2 + side3 > side1)
            &&(side3 + side1 > side2)) {  
        printf("It is a Valid Triangle\n");  
    } else {  
        printf("It is an invalid Triangle");  
    }  
  
    return 0;  
}

Output
Enter Length of Three Sides of a Triangle
10 20 20
It is a Valid Triangle
Enter Length of Three Sides of a Triangle
10 20 40
It is an invalid Triangle

Related Topics
C program to check whether three angles of triangle is valid or not
C Program to Check Whether a Triangle is Equilateral, Isosceles or Scalene Triangle
C Program to Calculate Area of a Right Angled Triangle
C Program to Calculate Area of Any Triangle using Heron's Formula
C Program to Find Third Angle of a Triangle
C Program to Print Floyd's Triangle
C Program to Print Triangle and Pyramid patterns of Star(*) Character Using Loop
C Program to Print Pascal Triangle till N Rows
C Program to Calculate Area and Perimeter of an Equilateral Triangle
List of all C programs