Check if a right-angled triangle can be formed by the given side lengths
Given two positive integers A and B representing the sides of a triangle, the task is to check if the given two sides of the triangle are sides of a valid right-angled triangle or not. If found to be true, print “YES“. Otherwise, print “No”.
Examples:
Input: A = 3, B = 4
Output: Yes
Explanation: A right-angled triangle is possible with side lengths 3, 4 and 5.
Input : A = 2, B = 5
Output: No
Approach: Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int checkPerfectSquare( int N)
{
if (N <= 0) {
return 0;
}
double sq = sqrt (N);
if ( floor (sq) == ceil (sq)) {
return 1;
}
return 0;
}
bool checktwoSidesareRighTriangle( int A, int B)
{
bool checkTriangle = false ;
if (checkPerfectSquare(A * A + B * B)) {
checkTriangle = true ;
}
if (checkPerfectSquare(A * A - B * B)) {
checkTriangle = true ;
}
if (checkPerfectSquare(B * B - A * A)) {
checkTriangle = true ;
}
return checkTriangle;
}
int main()
{
int A = 3, B = 4;
if (checktwoSidesareRighTriangle(A, B)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static int checkPerfectSquare( int N)
{
if (N <= 0 )
{
return 0 ;
}
double sq = Math.sqrt(N);
if (Math.floor(sq) == Math.ceil(sq))
{
return 1 ;
}
return 0 ;
}
static boolean checktwoSidesareRighTriangle( int A,
int B)
{
boolean checkTriangle = false ;
if (checkPerfectSquare(A * A + B * B) != 0 )
{
checkTriangle = true ;
}
if (checkPerfectSquare(A * A - B * B) != 0 )
{
checkTriangle = true ;
}
if (checkPerfectSquare(B * B - A * A) != 0 )
{
checkTriangle = true ;
}
return checkTriangle;
}
public static void main(String[] args)
{
int A = 3 , B = 4 ;
if (checktwoSidesareRighTriangle(A, B))
{
System.out.print( "Yes" );
}
else
{
System.out.print( "No" );
}
}
}
|
Python3
from math import sqrt, floor, ceil
def checkPerfectSquare(N):
if (N < = 0 ):
return 0
sq = sqrt(N)
if (floor(sq) = = ceil(sq)):
return 1
return 0
def checktwoSidesareRighTriangle(A, B):
checkTriangle = False
if (checkPerfectSquare(A * A + B * B)):
checkTriangle = True
if (checkPerfectSquare(A * A - B * B)):
checkTriangle = True
if (checkPerfectSquare(B * B - A * A)):
checkTriangle = True
return checkTriangle
if __name__ = = '__main__' :
A = 3
B = 4
if (checktwoSidesareRighTriangle(A, B)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static int checkPerfectSquare( int N)
{
if (N <= 0)
{
return 0;
}
double sq = Math.Sqrt(N);
if (Math.Floor(sq) == Math.Ceiling(sq))
{
return 1;
}
return 0;
}
static bool checktwoSidesareRighTriangle( int A,
int B)
{
bool checkTriangle = false ;
if (checkPerfectSquare(A * A + B * B) != 0)
{
checkTriangle = true ;
}
if (checkPerfectSquare(A * A - B * B) != 0)
{
checkTriangle = true ;
}
if (checkPerfectSquare(B * B - A * A) != 0)
{
checkTriangle = true ;
}
return checkTriangle;
}
public static void Main()
{
int A = 3, B = 4;
if (checktwoSidesareRighTriangle(A, B))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function checkPerfectSquare( N) {
if (N <= 0) {
return 0;
}
let sq = Math.sqrt(N);
if (Math.floor(sq) == Math.ceil(sq)) {
return 1;
}
return 0;
}
function checktwoSidesareRighTriangle( A , B) {
let checkTriangle = false ;
if (checkPerfectSquare(A * A + B * B) != 0) {
checkTriangle = true ;
}
if (checkPerfectSquare(A * A - B * B) != 0) {
checkTriangle = true ;
}
if (checkPerfectSquare(B * B - A * A) != 0) {
checkTriangle = true ;
}
return checkTriangle;
}
let A = 3, B = 4;
if (checktwoSidesareRighTriangle(A, B)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
|
Time Complexity: O(log(max(A, B))
Auxiliary Space: O(1)
Last Updated :
01 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...