c^2=a^2+b^2.
Example:
Consider the given right-angled triangle with side lengths a = 3 units and b = 4 units. The Pythagorean theorem is applied to calculate the length of the hypotenuse (c):
c^2 = 3^2 + 4^2
c^2 = 9 + 16
c^2 = 25
We can determine 'c' by finding the square root of both sides. This indicates that the length of the hypotenuse is 5 units.
Program:
Let's consider a C program to calculate the hypotenuse of a right-angled triangle.
Example
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c;
printf("Enter the side 'a': ");
scanf("%lf", &a);
printf("Enter the side 'b': ");
scanf("%lf", &b);
c = sqrt(a * a + b * b);
printf("The length of the Hypotenuse is: %.2lf\n", c);
return 0;
}
Output:
Output
Enter the side 'a': 3
Enter the side 'b': 4
The length of the Hypotenuse is: 5.00
Enter the side 'a': 21
Enter the side 'b': 28
The length of the Hypotenuse is: 35.00
Explanation:
- Include Libraries: In this example, we begin with including the necessary libraries. This program requires <h> for input and output functions, and <math.h> for mathematical computations like the square root (sqrt) function.
- Main Function: We declare the primary functionas the program's entry point.
- Variable Declaration: We define three double elements: a, b, and c. These variables will be used to hold the lengths of the two smaller sides (an and b) and the end of the hypotenuse (c).
- Input: We ask the user to enter the dimensions of the two smaller sides (aand b) with the printf and scanf The %lf format specifier acts for reading and storing double values.
- Calculation: We use the Pythagorean theorem to compute the hypotenuse length (c): c = sqrt(a a + b b) . We first square a and b, add them, and then use the sqrt method from the <math.h> library to get the square root.
- Output: The printf function is used to display the estimated length of the hypotenuse. Using the %.2lf format specifier, the result is presented with two decimal places.
- Return: Finally, the primary function returns 0, indicating that the program was successfully executed.