Find the hypotenuse of a right angled triangle with given two sides
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++
// 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 triangle double findHypotenuse(double side1, double side2) { double h = sqrt((side1 * side1) + (side2 * side2)); return h; } // Driver code int main() { int side1 = 3, side2 = 4; cout << fixed << showpoint; cout << setprecision(2); cout << findHypotenuse(side1, side2); } // This code is contributed by // Surendra_Gangwar |
Java
// Java implementation of the approach 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(String s[]) { int side1 = 3, side2 = 4; System.out.printf("%.2f", findHypotenuse(side1, side2)); } } |
Python3
# Python implementation of the approach # Function to return the hypotenuse of the # right angled triangle def findHypotenuse(side1, side2): h = (((side1 * side1) + (side2 * side2))**(1/2)); return h; # Driver code side1 = 3; side2 = 4; print(findHypotenuse(side1, side2)); # This code contributed by Rajput-Ji |
C#
// C# implementation of the approach using 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 |
5.00
Recommended Posts:
- Find all sides of a right angled triangle from given hypotenuse and area | Set 1
- Check whether right angled triangle is valid or not for large sides
- Find the dimensions of Right angled triangle
- Find the height of a right-angled triangle whose area is X times its base
- Find other two sides of a right angle triangle
- Find other two sides and angles of a right angle triangle
- Check if right triangle possible from given area and hypotenuse
- Area of Incircle of a Right Angled Triangle
- Check whether triangle is valid or not if sides are given
- Find all possible triangles with XOR of sides zero
- Find all angles of a given triangle
- Find Perimeter of a triangle
- Program to Find the Incenter of a Triangle
- Program to find area of a triangle
- Program to find the Centroid of the triangle
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



