C++ Program Using Pythagorean Theorem to Calculate Hypotenuse

by matt392 in Circuits > Software

9 Views, 0 Favorites, 0 Comments

C++ Program Using Pythagorean Theorem to Calculate Hypotenuse

abhypotenuse.png

C++ program that calculates the hypotenuse of a triangle using the Pythagorean Theorem.

Supplies

Cb_splash.png
mingw.png
gccegg-65.png
  1. CodeBlocks IDE with MinGW GCC Compiler: https://www.codeblocks.org/
  2. Windows computer

C++ Code

triangle01.png

#include <iostream>
#include <cmath>
using namespace std;
// Calculate the hypotenuse of a triangle
// using the Pythagorean Theorem
// Formula is: [a squared] + [b squared] = [c squared]
// c = square root ([a squared] + [b squared])
int main() {
// Declare variables for each side of the triangle
double sideA;
double sideB;
double sideC;

cout << "Enter side A of the triangle: " << endl;
cin >> sideA;
cout << "Enter side B of the triangle: " << endl;
cin >> sideB;
// Take the square root of (a squared) + (b squared)
// Use the power and square root functions in the cmath library
sideC = sqrt(pow(sideA,2) + pow(sideB,2));
cout << "The hypotenuse of the triangle is: " << sideC << endl;

return 0;
}