Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* LAB 8 – Pythagoras Lives!!!
- Write a program that will calculate the hypotenuse of a right triangle given the
- two leg sides from the user.
- Be sure to use functions. Ensure that the calculation function you write has type
- double for its arguments and return type.
- You may use a new precompiled function found in the <math.h> header file. It
- is prototyped as follows:
- double sqrt (double x);
- This function will find the square root of some variable or expression ‘x’ and return the
- square root approximation as a double.
- Display the output in a well formatted easy to read table. Work that printf()
- PSEUDOCODE
- 1) The first leg of the triangle, the second leg of the triangle
- 2) double leg1, leg2;
- 3 & 4) I. Get user input
- A. prompt user using printf() and use scanf() to load values into leg1 and leg2
- II. Perform calculations
- A. create a new function hypotenuse to perform the calculations in
- B. use sqrt(pow(legone,2)+pow(legtwo, 2)) to calculate the hypotenuse
- III. Display the results
- A. use printf() and field width specifiers to display results in a formatted table
- */
- #include <stdio.h>
- #include <math.h>
- double hypotenuse();
- int main()
- {
- double leg1, leg2, hypval;
- printf("Enter the first leg: ");
- scanf("%lf", &leg1);
- printf("Enter the second leg: ");
- scanf("%lf", &leg2);
- hypval = hypotenuse(leg1, leg2);
- printf("%11s%11s%12s\n", "Leg one", "Leg two", "Hypotenuse");
- printf("%11lf%11lf%12lf\n", leg1, leg2, hypval);
- return 0;
- }
- double hypotenuse(double legone, double legtwo)
- {
- double hyp;
- hyp = sqrt(pow(legone,2)+pow(legtwo,2));
- return hyp;
- }
Add Comment
Please, Sign In to add comment