Dados los otros dos lados de un triángulo rectángulo, la tarea es encontrar su hipotenusa.
Ejemplos:
Entrada: lado1 = 3, lado2 = 4
Salida: 5,00
3 2 + 4 2 = 5 2
Entrada: lado1 = 12, lado2 = 15
Salida: 19,21
Planteamiento: El teorema de Pitágoras establece que el cuadrado de la hipotenusa de un triángulo rectángulo es igual a la suma de los cuadrados de los otros dos lados.
A continuación se muestra la implementación del enfoque anterior:
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
Javascript
<script> // java script implementation of the approach // Function to return the hypotenuse of the //right angled triangle function findHypotenuse(side1, side2){ let h = (((side1 * side1) + (side2 * side2))**(1/2)); return h; } // Driver code let side1 = 3; let side2 = 4; document.write(findHypotenuse(side1, side2).toFixed(2)); // This code is contributed by Gottumukkala Bobby </script>
Producción:
5.00
Complejidad de tiempo : O(log(2*(s 2 )) donde s es el lado del rectángulo.
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Vaibhav_Arora y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA