# Check if a right-angled triangle can be formed by the given side lengths

Last Updated : 01 Oct, 2021

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

 // C++ program to implement // the above approach   #include 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; }

## Java

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

## Python3

 # 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

## C#

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

## Javascript



Output:

Yes

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