Check whether the triangle is valid or not if angles are given
Given three integers A, B and C which are the three angles of a possible triangle in degrees, the task is to check whether the triangle is valid or not.
Examples:
Input: A = 60, B = 40, C = 80
Output: Valid
Input: A = 55, B = 45, C = 60
Output: Invalid
Approach: A triangle is valid if the sum of the three angles is equal to 180 degrees.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool Valid( int a, int b, int c)
{
if (a + b + c == 180 && a != 0 && b != 0 && c != 0)
return true ;
else
return false ;
}
int main()
{
int a = 60, b = 40, c = 80;
if (Valid(a, b, c))
cout << "Valid" ;
else
cout << "Invalid" ;
}
|
Java
class GFG {
public static int Valid( int a, int b, int c)
{
if (a + b + c == 180 && a != 0 && b != 0 && c != 0 )
return 1 ;
else
return 0 ;
}
public static void main(String args[])
{
int a = 60 , b = 40 , c = 80 ;
if ((Valid(a, b, c)) == 1 )
System.out.print( "Valid" );
else
System.out.print( "Invalid" );
}
}
|
Python3
def Valid(a, b, c):
if ((a + b + c = = 180 ) and a ! = 0 and b ! = 0 and c ! = 0 ):
return True
else :
return False
if __name__ = = "__main__" :
a = 60
b = 40
c = 80
if (Valid(a, b, c)):
print ( "Valid" )
else :
print ( "Invalid" )
|
C#
using System;
class GFG {
public static int Valid( int a, int b, int c)
{
if (a + b + c == 180 && a != 0 && b != 0 && c != 0)
return 1;
else
return 0;
}
public static void Main()
{
int a = 60, b = 40, c = 80;
if ((Valid(a, b, c)) == 1)
Console.WriteLine( "Valid" );
else
Console.WriteLine( "Invalid" );
}
}
|
Javascript
function Valid(a, b, c)
{
if (a + b + c == 180 && a != 0 && b != 0 && c != 0)
return 1;
else
return 0;
}
var a = 60, b = 40, c = 80;
if ((Valid(a, b, c)) == 1){
document.write( "Valid" );
}
else {
document.write( "Invalid" );
}
|
Output:
Valid
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
04 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...