Introduction
In this lab, you will learn how to compute the hypotenuse of a right triangle using C programming. The lab covers the step-by-step process of reading the lengths of the two legs, and then calculating the hypotenuse using the Pythagorean theorem. By the end of this lab, you will have a solid understanding of how to perform trigonometric calculations in C.
The lab begins by guiding you through the process of reading the lengths of the two legs of the right triangle. You will then use the Pythagorean theorem, which states that the square of the hypotenuse is equal to the sum of the squares of the other two sides, to calculate the hypotenuse. Finally, you will learn how to print the calculated hypotenuse.
Read Two Legs of a Right Triangle
In this step, you will learn how to read the two legs of a right triangle in a C program. We'll create a simple program that captures user input for the lengths of the triangle's legs.
First, navigate to the project directory and create a new C file:
cd ~/project
nano hypotenuse.c
Now, add the following code to read the two legs of the right triangle:
#include <stdio.h>
int main() {
float leg1, leg2;
printf("Enter the length of the first leg: ");
scanf("%f", &leg1);
printf("Enter the length of the second leg: ");
scanf("%f", &leg2);
printf("First leg: %.2f\n", leg1);
printf("Second leg: %.2f\n", leg2);
return 0;
}
Let's break down the code:
- We use
floatto allow decimal numbers for leg lengths printf()is used to prompt the user for inputscanf()reads the user's input and stores it in the variablesleg1andleg2- We then print out the entered leg lengths to confirm input
Compile the program:
gcc hypotenuse.c -o hypotenuse
Example output when running the program:
Enter the length of the first leg: 3
Enter the length of the second leg: 4
First leg: 3.00
Second leg: 4.00
Use Pythagorean Theorem c = sqrt(a² + b²)
In this step, you will modify the previous program to calculate the hypotenuse using the Pythagorean theorem. We'll use the sqrt() function from the math library to compute the square root.
Open the existing file and update the code:
cd ~/project
nano hypotenuse.c
Replace the previous code with the following implementation:
#include <stdio.h>
#include <math.h>
int main() {
float leg1, leg2, hypotenuse;
printf("Enter the length of the first leg: ");
scanf("%f", &leg1);
printf("Enter the length of the second leg: ");
scanf("%f", &leg2);
// Calculate hypotenuse using Pythagorean theorem
hypotenuse = sqrt(leg1 * leg1 + leg2 * leg2);
printf("First leg: %.2f\n", leg1);
printf("Second leg: %.2f\n", leg2);
printf("Hypotenuse: %.2f\n", hypotenuse);
return 0;
}
Compile the program with the math library:
gcc hypotenuse.c -o hypotenuse -lm
Example output when running the program:
Enter the length of the first leg: 3
Enter the length of the second leg: 4
First leg: 3.00
Second leg: 4.00
Hypotenuse: 5.00
Key points about the code:
#include <math.h>adds the math library forsqrt()functionsqrt(leg1 * leg1 + leg2 * leg2)calculates the hypotenuse-lmflag is used during compilation to link the math library
Print the Hypotenuse
In this step, you will enhance the previous program to provide a more formatted and user-friendly output of the hypotenuse calculation.
Open the existing file and update the code:
cd ~/project
nano hypotenuse.c
Modify the code to improve output formatting:
#include <stdio.h>
#include <math.h>
int main() {
float leg1, leg2, hypotenuse;
printf("Right Triangle Hypotenuse Calculator\n");
printf("------------------------------------\n");
printf("Enter the length of the first leg: ");
scanf("%f", &leg1);
printf("Enter the length of the second leg: ");
scanf("%f", &leg2);
// Calculate hypotenuse using Pythagorean theorem
hypotenuse = sqrt(leg1 * leg1 + leg2 * leg2);
// Print formatted results
printf("\nTriangle Measurements:\n");
printf("First Leg: %.2f\n", leg1);
printf("Second Leg: %.2f\n", leg2);
printf("Hypotenuse: %.2f\n", hypotenuse);
// Additional calculation details
printf("\nCalculation Details:\n");
printf("Formula: c = √(a² + b²)\n");
printf(" = √(%.2f² + %.2f²)\n", leg1, leg2);
printf(" = √(%.2f + %.2f)\n", leg1 * leg1, leg2 * leg2);
printf(" = %.2f\n", hypotenuse);
return 0;
}
Compile the program:
gcc hypotenuse.c -o hypotenuse -lm
Run the program and see the detailed output:
./hypotenuse
Example output:
Right Triangle Hypotenuse Calculator
------------------------------------
Enter the length of the first leg: 3
Enter the length of the second leg: 4
Triangle Measurements:
First Leg: 3.00
Second Leg: 4.00
Hypotenuse: 5.00
Calculation Details:
Formula: c = √(a² + b²)
= √(3.00² + 4.00²)
= √(9.00 + 16.00)
= 5.00
Key improvements:
- Added descriptive headers
- Formatted output with aligned columns
- Included step-by-step calculation details
- Improved readability for users
Summary
In this lab, you first learned how to read the two legs of a right triangle in a C program by prompting the user for input and storing the values in variables. You then used the Pythagorean theorem, which states that the square of the hypotenuse is equal to the sum of the squares of the other two sides, to calculate the hypotenuse. The program utilizes the sqrt() function from the math library to compute the square root and determine the length of the hypotenuse.



