Complementary task for topic: 4
M Nemeth · 2023-08-29 15:21:04.619219'
Calculate Hypotenuse of a Right-Angled Triangle
Calculate Hypotenuse of a Right-Angled Triangle
Write a C function to calculate the hypotenuse of a right-angled triangle. The function should take the lengths of the two sides (a and b) as floating-point inputs and return the length of the hypotenuse as a floating-point number. Use the formula: Hypotenuse = sqrt(a^2 + b^2).
Hint:
Solution
#include
#include
// Function to calculate the hypotenuse of a right-angled triangle
float calculate_hypotenuse(float a, float b) {
return sqrt(a * a + b * b);
}
int main() {
float a, b;
printf("Enter the lengths of the two sides of the right-angled triangle: ");
scanf("%f %f", &a, &b);
float hypotenuse = calculate_hypotenuse(a, b);
printf("The length of the hypotenuse is %.2f.\n", hypotenuse);
return 0;
}
Explanation
The calculate_hypotenuse function takes the lengths of the two sides (a and b) of a right-angled triangle as input and returns the length of the hypotenuse using the Pythagorean theorem: Hypotenuse = sqrt(a^2 + b^2). In the main function, the user is asked to input the lengths of the two sides, and the calculate_hypotenuse function is called to calculate and print the length of the hypotenuse.