> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/c++-notes/10.-practice-program-hypotenuse-calculator.md).

# 10. Practice Program -Hypotenuse Calculator

This section is a practice project that combines everything from the earlier topics: **user input** (topic 08) and **math functions** (topic 09). We'll build a small program that calculates the hypotenuse of a right-angled triangle using the Pythagorean theorem:

```
c = √(a² + b²)
```

Where `a` and `b` are the two shorter sides, and `c` is the hypotenuse (the longest side, opposite the right angle).

***

### The full program (your style — reusing `a`, `b`, `c`)

Instead of a separate `hypotenuse` variable, this version reuses `a` and `b` themselves to store the squared values, then reuses `c` to hold the final result. Fewer variable names, same math.

```cpp
#include <iostream>
#include <cmath>

int main() {
    double a, b, c;

    std::cout << "Enter size A: ";
    std::cin >> a;

    std::cout << "Enter size B: ";
    std::cin >> b;

    a = pow(a, 2);
    b = pow(b, 2);
    c = sqrt(a + b);

    std::cout << "The hypotenuse is: " << c << '\n';

    return 0;
}
```

**Example run:**

```
Enter size A: 3
Enter size B: 4
The hypotenuse is: 5
```

***

### Breaking it down

| Step               | What's happening                                                                                                                 |
| ------------------ | -------------------------------------------------------------------------------------------------------------------------------- |
| `double a, b, c;`  | Declare all three up front — `a` and `b` for input, `c` for the result.                                                          |
| `std::cin >> a;`   | Read side A from the user (see topic 08).                                                                                        |
| `std::cin >> b;`   | Read side B from the user.                                                                                                       |
| `a = pow(a, 2);`   | Overwrite `a` with its own square (`a²`). Notice `a` no longer holds the raw side length after this line — it's been repurposed. |
| `b = pow(b, 2);`   | Same idea — overwrite `b` with `b²`.                                                                                             |
| `c = sqrt(a + b);` | Add the two squared values and take the square root — the Pythagorean theorem.                                                   |
| `std::cout << c`   | Print the final hypotenuse.                                                                                                      |

**Worth knowing:** reassigning `a = pow(a, 2)` works fine here because we don't need the *original* side length again after squaring it. This is a common, perfectly valid pattern for short scripts — but in bigger programs it's often clearer to use separate variable names (like `sideA`, `sideASquared`) so nobody gets confused about what a variable currently holds mid-program.

This is exactly why topics 08 and 09 come before this one — you need to **get input from the user** and **know your math functions** to build something like this.

***

### More cool examples (variations on the same idea)

#### 1. Using `*` instead of `pow` (an alternative approach)

`pow(x, 2)` works, but for squaring specifically, plain multiplication is often simpler and avoids `pow`'s `double`-return quirks:

```cpp
#include <iostream>
#include <cmath>

int main() {
    double sideA, sideB;

    std::cout << "Enter side A: ";
    std::cin >> sideA;

    std::cout << "Enter side B: ";
    std::cin >> sideB;

    double hypotenuse = sqrt((sideA * sideA) + (sideB * sideB));

    std::cout << "Hypotenuse: " << hypotenuse << '\n';

    return 0;
}
```

#### 2. Rounding the result to 2 decimal places (feels more "finished")

```cpp
#include <iostream>
#include <cmath>
#include <iomanip> // for setprecision

int main() {
    double sideA, sideB;

    std::cout << "Enter side A: ";
    std::cin >> sideA;

    std::cout << "Enter side B: ";
    std::cin >> sideB;

    double hypotenuse = sqrt(pow(sideA, 2) + pow(sideB, 2));

    std::cout << std::fixed << std::setprecision(2);
    std::cout << "Hypotenuse: " << hypotenuse << '\n';

    return 0;
}
```

#### 3. Finding a missing side instead (rearranged formula)

If you know the hypotenuse and one side, you can find the missing side: `a = √(c² - b²)`

```cpp
#include <iostream>
#include <cmath>

int main() {
    double hypotenuse, sideB;

    std::cout << "Enter the hypotenuse: ";
    std::cin >> hypotenuse;

    std::cout << "Enter side B: ";
    std::cin >> sideB;

    double sideA = sqrt(pow(hypotenuse, 2) - pow(sideB, 2));

    std::cout << "Side A: " << sideA << '\n';

    return 0;
}
```

#### 4. Adding simple input validation

```cpp
#include <iostream>
#include <cmath>

int main() {
    double sideA, sideB;

    std::cout << "Enter side A: ";
    std::cin >> sideA;

    std::cout << "Enter side B: ";
    std::cin >> sideB;

    if (sideA <= 0 || sideB <= 0) {
        std::cout << "Sides must be positive numbers!\n";
        return 1; // non-zero return = program exited with an error
    }

    double hypotenuse = sqrt(pow(sideA, 2) + pow(sideB, 2));
    std::cout << "Hypotenuse: " << hypotenuse << '\n';

    return 0;
}
```

***

### Quick Recap

* This program is a hands-on combo of **topic 08 (user input)** + **topic 09 (math functions)**.
* Formula: `c = sqrt(a² + b²)` — implemented with `sqrt()` and `pow()` from `<cmath>`.
* You can square with `pow(x, 2)` or simply `x * x` — both work, multiplication is often clearer for squaring.
* Good practice extensions: rounding output, validating input, or rearranging the formula to solve for a different side.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://alham-rizvi.gitbook.io/alhamrizvi/c++-notes/10.-practice-program-hypotenuse-calculator.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
