📜  如果给出边,检查三角形是否有效

📅  最后修改于: 2021-10-23 08:07:25             🧑  作者: Mango

给定三个边,检查三角形是否有效。
例子:

Input :  a = 7, b = 10, c = 5 
Output : Valid

Input : a = 1 b = 10 c = 12 
Output : Invalid

方法:如果三角形的两条边之和大于第三条边,则三角形是有效的。如果三边是a、b、c,则应满足三个条件。

1.a + b > c 
2.a + c > b 
3.b + c > a  

C++
// C++ program to check if three
// sides form a triangle or not
#include
using namespace std;
 
// function to check if three sider
// form a triangle or not
bool 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 function
int main()
{
    int a = 7, b = 10, c = 5;
     
    if (checkValidity(a, b, c))
        cout << "Valid";
    else
        cout << "Invalid";   
}


Java
// 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 article is contributed by 'Akansh Gupta'


Python3
# Python3 program to check if three
# sides form a  triangle or not
 
# function to check if three sides
# form a triangle or not
def checkValidity(a, b, c):
     
    # check condition
    if (a + b <= c) or (a + c <= b) or (b + c <= a) :
        return False
    else:
        return True       
 
# driver code
a = 7
b = 10
c = 5
if checkValidity(a, b, c):
    print("Valid")
else:
    print("Invalid")


C#
// C# program to check
// validity of any triangle
using 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


Javascript


输出 :

Valid