Find the hypotenuse of a right angled triangle with given two sides

21 Jun, 2022
Given the other two sides of a right angled triangle, the task is to find it’s hypotenuse.Examples:  
Input: side1 = 3, side2 = 4 Output: 5.00 32 + 42 = 52Input: side1 = 12, side2 = 15 Output: 19.21  
 
Approach: Pythagoras theorem states that the square of hypotenuse of a right angled triangle is equal to the sum of squares of the other two sides.Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include<bits/stdc++.h>#include <iostream>#include <iomanip>using namespace std; // Function to return the hypotenuse of the// right angled triangledouble findHypotenuse(double side1, double side2){    double h = sqrt((side1 * side1) + (side2 * side2));    return h;} // Driver codeint main(){    int side1 = 3, side2 = 4;    cout << fixed << showpoint;    cout << setprecision(2);    cout << findHypotenuse(side1, side2);}     // This code is contributed by// Surendra_Gangwar
// Java implementation of the approachclass GFG {     // Function to return the hypotenuse of the    // right angled triangle    static double findHypotenuse(double side1, double side2)    {        double h = Math.sqrt((side1 * side1) + (side2 * side2));        return h;    }     // Driver code    public static void main(String s[])    {        int side1 = 3, side2 = 4;        System.out.printf("%.2f", findHypotenuse(side1, side2));    }}
# Python implementation of the approach # Function to return the hypotenuse of the# right angled triangledef findHypotenuse(side1, side2):     h = (((side1 * side1) + (side2 * side2))**(1/2));    return h; # Driver codeside1 = 3;side2 = 4; print(findHypotenuse(side1, side2)); # This code contributed by Rajput-Ji
// C# implementation of the approachusing System;     class GFG{     // Function to return the hypotenuse    // of the right angled triangle    static double findHypotenuse(double side1,                                 double side2)    {        double h = Math.Sqrt((side1 * side1) +                             (side2 * side2));        return h;    }     // Driver code    public static void Main()    {        int side1 = 3, side2 = 4;        Console.Write("{0:F2}", findHypotenuse(side1,                                               side2));    }} // This code is contributed// by Princi Singh
<script>// java script  implementation of the approach // Function to return the hypotenuse of the//right angled trianglefunction findHypotenuse(side1, side2){     let h = (((side1 * side1) + (side2 * side2))**(1/2));    return h;} // Driver codelet side1 = 3;let side2 = 4; document.write(findHypotenuse(side1, side2).toFixed(2)); // This code is contributed by Gottumukkala Bobby</script>
5.00
 
Time Complexity: O(log(2*(s2)) where s is the side of the rectangle.
Auxiliary Space: O(1)
SURENDRA_GANGWAR
princi singh
Rajput-Ji
gottumukkalabobby
krishnav4
triangle
Java Programs
School Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Factory method design pattern in Java
Java Program to Remove Duplicate Elements From the Array
Iterate through List in Java
Java program to count the occurrence of each character in a string using Hashmap
How to Iterate HashMap in Java?
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java