Project: Hypotenuse Calculator
Welcome to your first project! We're going to build a program that calculates the hypotenuse of a right triangle using the Pythagorean theorem.
The Pythagorean theorem states: a² + b² = c²
, where c
is the hypotenuse.
To find the hypotenuse: c = √(a² + b²)
We'll need to:
- Get the lengths of the two sides from the user
- Calculate the squares of both sides
- Add them together
- Take the square root of the sum
Step 1: Set Up the Program
First, let's include the necessary libraries and set up our main function. We'll need <iostream>
for input/output and <cmath>
for the square root function.
Step 2: Declare Variables
Now we need to declare variables to store our triangle's side lengths and the calculated hypotenuse.
We'll use double
for all variables since we're dealing with measurements that might have decimal places.
Your Task
Declare three double
variables: sideA
, sideB
, and hypotenuse
.
Step 3: Get User Input
Now let's get the lengths of the two sides from the user.
Remember to provide clear prompts so the user knows what to enter!
Your Task
Add prompts and std::cin
statements to get the values for sideA
and sideB
from the user.
Step 4: Calculate the Hypotenuse
Now for the math! We need to:
- Square both sides:
sideA * sideA
and sideB * sideB
- Add the squares together
- Take the square root using
sqrt()
Your Task
Calculate the hypotenuse using the Pythagorean theorem and store the result in the hypotenuse
variable.
Step 5: Display the Result
Finally, let's show the user the calculated hypotenuse!
Make the output clear and informative so the user understands what the result means.
Your Task
Display the calculated hypotenuse to the user with a clear message.
🎉
Project Complete!
Congratulations! You've successfully built your first C++ program that combines multiple concepts: variables, user input, arithmetic operations, math functions, and output.
Back to Curriculum