139k views
0 votes
Write a function hypotenuse (a,b) to calculate the hypotenuse of a right-angled triangle, where a and b are the lengths of the other triangle sides. The program should calculate the length c of the hypotenuse given the formula: c=sqrt(a² +b² ). The function should return value c. Note: For testing a=3 cm and b=4 cm can be used.

User Mindmyweb
by
7.8k points

1 Answer

6 votes

Final answer:

To calculate the hypotenuse of a right-angled triangle using the Pythagorean theorem, the function hypotenuse(a, b) takes in the lengths of sides a and b, and returns the square root of their squares added together (√(a² + b²)). When tested with a=3 cm and b=4 cm, the function will correctly return 5 cm for the hypotenuse.

Step-by-step explanation:

The function hypotenuse(a, b) is designed to calculate the length of the hypotenuse (c) of a right-angled triangle using the Pythagorean theorem. The theorem states that the square of the hypotenuse is equal to the sum of the squares of the other two sides.

Given side lengths a and b, the length of the hypotenuse c is calculated as follows: c = √(a² + b²).To implement this in a programming context, one could define a function like so: function hypotenuse(a, b){ return Math. sqrt(a * a + b * b);} Using the provided test values a=3 cm and b=4 cm, the function would return the hypotenuse calculated as: hypotenuse(3, 4); // returns 5 This is because the square root of (3² + 4²) is the square root of (9 + 16), which is the square root of 25, resulting in hypotenuse length of 5 cm.

User Phydeaux
by
7.0k points