Check whether triangle is valid or not if sides are given - GeeksforGeeks

19 Apr, 2022
Given three sides, check whether triangle is valid or not. Examples:  
Input :  a = 7, b = 10, c = 5 
Output : Valid

Input : a = 1 b = 10 c = 12 
Output : Invalid
 
Approach: A triangle is valid if sum of its two sides is greater than the third side. If three sides are a, b and c, then three conditions should be met.  
1.a + b > c 
2.a + c > b 
3.b + c > a  
 
 
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program to check if three sides form a triangle or not#include <bits/stdc++.h>using namespace std; // function to check if three sider form a triangle or notbool checkValidity(int a, int b, int c){    // check condition    if (a + b <= c || a + c <= b || b + c <= a)        return false;    else        return true;} // Driver functionint main(){    int a = 7, b = 10, c = 5;    if (checkValidity(a, b, c))        cout << "Valid";    else        cout << "Invalid";} // This code is contributed by Aditya Kumar (adityakumar129)
// C program to check if three sides form a triangle or not#include <stdio.h> // function to check if three sider form a triangle or notbool checkValidity(int a, int b, int c){    // check condition    if (a + b <= c || a + c <= b || b + c <= a)        return false;    else        return true;} // Driver functionint main(){    int a = 7, b = 10, c = 5;    if (checkValidity(a, b, c))        printf("Valid");    else        printf("Invalid");} // This code is contributed by Aditya Kumar (adityakumar129)
// Java program to check validity of any triangle public class GFG {    // Function to calculate for validity    public static int checkValidity(int a, int b, int c)    {        // check condition        if (a + b <= c || a + c <= b || b + c <= a)            return 0;        else            return 1;    }     // Driver function    public static void main(String args[])    {        int a = 7, b = 10, c = 5;        // function calling and print output        if ((checkValidity(a, b, c)) == 1)            System.out.print("Valid");        else            System.out.print("Invalid");    }} // This code is contributed by Aditya Kumar (adityakumar129)
# Python3 program to check if three# sides form a  triangle or not # function to check if three sides# form a triangle or notdef checkValidity(a, b, c):         # check condition    if (a + b <= c) or (a + c <= b) or (b + c <= a) :        return False    else:        return True        # driver codea = 7b = 10c = 5if checkValidity(a, b, c):    print("Valid")else:    print("Invalid")
// C# program to check// validity of any triangleusing System; class GFG {         // Function to calculate for validity    public static int checkValidity(int a, int b,                                    int c)    {                 // check condition        if (a + b <= c || a + c <= b ||                            b + c <= a)            return 0;        else            return 1;    }     // Driver code    public static void Main()    {        int a = 7, b = 10, c = 5;             // function calling and print output        if ((checkValidity(a, b, c)) == 1)          Console.Write("Valid");        else          Console.Write("Invalid");             }} // This code is contributed by Nitin Mittal.
<?php// PHP program to check if three// sides form a triangle or not // function to check if three sider// form a triangle or notfunction checkValidity($a, $b, $c){         // check condition    if ($a + $b <= $c ||        $a + $c <= $b ||        $b + $c <= $a)        return false;    else        return true;}     // Driver Code    $a = 7;    $b = 10;    $c = 5;         if (checkValidity($a, $b, $c))        echo "Valid";    else        echo "Invalid";         // This code is contributed by nitin mittal.?>
<script> // Javascript program to check if three// sides form a triangle or not // function to check if three sider// form a triangle or notfunction checkValidity(a, b, c){    // check condition    if (a + b <= c || a + c <= b || b + c <= a)        return false;    else        return true;} // Driver function      let a = 7, b = 10, c = 5;         if (checkValidity(a, b, c))        document.write("Valid");    else        document.write("Invalid");     // This code is contributed by Mayank Tyagi </script>
Output :  
Valid
 
Koushik Mondal
nitin mittal
mayanktyagi1709
gaurav4037
adityakumar129
triangle
Geometric
School Programming
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program for distance between two points on earth
Given n line segments, find if any two segments intersect
Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
Closest Pair of Points | O(nlogn) Implementation
Polygon Clipping | Sutherland–Hodgman Algorithm
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java