Related Articles

Related Articles

Check if a right-angled triangle can be formed by the given side lengths
  • Last Updated : 17 Nov, 2020

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if N is a
// perfect square number or not
int checkPerfectSquare(int N)
{
 
    // If N is a non
    // positive integer
    if (N <= 0) {
        return 0;
    }
 
    // Stores square root
    // of N
    double sq = sqrt(N);
 
    // Check for perfect square
    if (floor(sq) == ceil(sq)) {
        return 1;
    }
 
    // If N is not a
    // perfect square number
    return 0;
}
 
// Function to check if given two sides of a
// triangle forms a right-angled triangle
bool checktwoSidesareRighTriangle(int A, int B)
{
    bool checkTriangle = false;
 
    // If the value of (A * A + B * B) is a
    // perfect square number
    if (checkPerfectSquare(A * A + B * B)) {
 
        // Update checkTriangle
        checkTriangle = true;
    }
 
    // If the value of (A * A - B * B) is a
    // perfect square number
    if (checkPerfectSquare(A * A - B * B)) {
 
        // Update checkTriangle
        checkTriangle = true;
    }
 
    // If the value of (B * B - A * A) is a
    // perfect square number
    if (checkPerfectSquare(B * B - A * A)) {
 
        // Update checkTriangle
        checkTriangle = true;
    }
 
    return checkTriangle;
}
 
// Driver Code
int main()
{
    int A = 3, B = 4;
 
    // If the given two sides of a triangle
    // forms a right-angled triangle
    if (checktwoSidesareRighTriangle(A, B)) {
        cout << "Yes";
    }
 
    // Otherwise
    else {
        cout << "No";
    }
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
   
class GFG{
   
// Function to check if N is a
// perfect square number or not
static int checkPerfectSquare(int N)
{
     
    // If N is a non
    // positive integer
    if (N <= 0)
    {
        return 0;
    }
  
    // Stores square root
    // of N
    double sq = Math.sqrt(N);
  
    // Check for perfect square
    if (Math.floor(sq) == Math.ceil(sq))
    {
        return 1;
    }
  
    // If N is not a
    // perfect square number
    return 0;
}
  
// Function to check if given two sides of a
// triangle forms a right-angled triangle
static boolean checktwoSidesareRighTriangle(int A,
                                            int B)
{
    boolean checkTriangle = false;
  
    // If the value of (A * A + B * B) is a
    // perfect square number
    if (checkPerfectSquare(A * A + B * B) != 0)
    {
         
        // Update checkTriangle
        checkTriangle = true;
    }
  
    // If the value of (A * A - B * B) is a
    // perfect square number
    if (checkPerfectSquare(A * A - B * B) != 0)
    {
         
        // Update checkTriangle
        checkTriangle = true;
    }
  
    // If the value of (B * B - A * A) is a
    // perfect square number
    if (checkPerfectSquare(B * B - A * A) != 0)
    {
         
        // Update checkTriangle
        checkTriangle = true;
    }
    return checkTriangle;
}
   
// Driver Code
public static void main(String[] args)
{
    int A = 3, B = 4;
  
    // If the given two sides of a triangle
    // forms a right-angled triangle
    if (checktwoSidesareRighTriangle(A, B))
    {
        System.out.print("Yes");
    }
  
    // Otherwise
    else
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by susmitakundugoaldanga

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
from math import sqrt, floor, ceil
 
# Function to check if N is a
# perfect square number or not
def checkPerfectSquare(N):
     
    # If N is a non
    # positive integer
    if (N <= 0):
        return 0
         
    # Stores square root
    # of N
    sq = sqrt(N)
     
    # Check for perfect square
    if (floor(sq) == ceil(sq)):
        return 1
         
    # If N is not a
    # perfect square number
    return 0
     
# Function to check if given two sides of a
# triangle forms a right-angled triangle
def checktwoSidesareRighTriangle(A, B):
     
    checkTriangle = False
     
    # If the value of (A * A + B * B) is a
    # perfect square number
    if (checkPerfectSquare(A * A + B * B)):
         
        # Update checkTriangle
        checkTriangle = True
 
    # If the value of (A * A - B * B) is a
    # perfect square number
    if (checkPerfectSquare(A * A - B * B)):
         
        # Update checkTriangle
        checkTriangle = True
 
    # If the value of (B * B - A * A) is a
    # perfect square number
    if (checkPerfectSquare(B * B - A * A)):
         
        # Update checkTriangle
        checkTriangle = True
 
    return checkTriangle
 
# Driver Code
if __name__ == '__main__':
     
    A = 3
    B = 4
     
    # If the given two sides of a triangle
    # forms a right-angled triangle
    if (checktwoSidesareRighTriangle(A, B)):
        print("Yes")
         
    # Otherwise
    else:
        print("No")
 
# This code is contributed by SURENDRA_GANGWAR

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement
// the above approach 
using System;
    
class GFG{
     
// Function to check if N is a
// perfect square number or not
static int checkPerfectSquare(int N)
{
     
    // If N is a non
    // positive integer
    if (N <= 0)
    {
        return 0;
    }
   
    // Stores square root
    // of N
    double sq = Math.Sqrt(N);
   
    // Check for perfect square
    if (Math.Floor(sq) == Math.Ceiling(sq))
    {
        return 1;
    }
   
    // If N is not a
    // perfect square number
    return 0;
}
   
// Function to check if given two sides of a
// triangle forms a right-angled triangle
static bool checktwoSidesareRighTriangle(int A,
                                         int B)
{
    bool checkTriangle = false;
   
    // If the value of (A * A + B * B) is a
    // perfect square number
    if (checkPerfectSquare(A * A + B * B) != 0)
    {
         
        // Update checkTriangle
        checkTriangle = true;
    }
   
    // If the value of (A * A - B * B) is a
    // perfect square number
    if (checkPerfectSquare(A * A - B * B) != 0)
    {
         
        // Update checkTriangle
        checkTriangle = true;
    }
   
    // If the value of (B * B - A * A) is a
    // perfect square number
    if (checkPerfectSquare(B * B - A * A) != 0)
    {
          
        // Update checkTriangle
        checkTriangle = true;
    }
    return checkTriangle;
}
  
// Driver Code
public static void Main()
{
    int A = 3, B = 4;
   
    // If the given two sides of a triangle
    // forms a right-angled triangle
    if (checktwoSidesareRighTriangle(A, B))
    {
        Console.Write("Yes");
    }
   
    // Otherwise
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by code_hunt

chevron_right


Output: 

Yes












 

Time Complexity: O(log(max(A, B))
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :