Friday, December 27, 2024
Google search engine
HomeData Modelling & AICheck whether triangle is valid or not if sides are given

Check whether triangle is valid or not if sides are given

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  

 

Check whether triangle is valid or not if sides are given

 

C++




// 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 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";
}
  
// This code is contributed by Aditya Kumar (adityakumar129)


C




// C program to check if three sides form a triangle or not
#include <stdio.h>
#include <stdbool.h>
  
// 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;
    return true;
}
  
// Driver function
void 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




// 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




# 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




<?php
// PHP program to check if three
// sides form a triangle or not
  
// function to check if three sider
// form a triangle or not
function 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.
?>


Javascript




<script>
  
// Javascript program to check if three 
// sides form a triangle or not 
  
// function to check if three sider 
// form a triangle or not 
function 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

Time Complexity: O(1)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments