C++ Program for Pythagorean Theorem
Introduction
The Pythagorean Theorem is one of the most fundamental concepts in geometry. It states that in a right-angled triangle, the square of the length of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the lengths of the other two sides. This theorem is widely used in mathematics, physics, engineering, and various real-world applications.
In this article, we will implement the Pythagorean Theorem in C++ to compute the hypotenuse when the lengths of the other two sides are given. We will also verify if three given sides form a right triangle.
Formula
The Pythagorean Theorem is expressed mathematically as:
-
For a right triangle:
c² = a² + b²
, where:c
is the length of the hypotenuse.a
andb
are the lengths of the other two sides.
-
Hypotenuse Calculation:
c = sqrt(a² + b²)
, wheresqrt
is the square root function. -
Verification of a Right Triangle: Given three sides
a
,b
, andc
(wherec
is the largest), the triangle is a right triangle if:c² = a² + b²
.
Code
Below is the C++ implementation of the Pythagorean Theorem:
#include <iostream> #include <cmath> using namespace std; int main() { int choice; cout << "Choose an option:" << endl; cout << "1. Calculate the hypotenuse" << endl; cout << "2. Verify if three sides form a right triangle" << endl; cin >> choice; if (choice == 1) { // Calculate the hypotenuse double sideA, sideB, hypotenuse; cout << "Enter the length of the first side (a): "; cin >> sideA; cout << "Enter the length of the second side (b): "; cin >> sideB; hypotenuse = sqrt(pow(sideA, 2) + pow(sideB, 2)); cout << "The length of the hypotenuse is: " << hypotenuse << endl; } else if (choice == 2) { // Verify if three sides form a right triangle double sideA, sideB, sideC; cout << "Enter the length of the first side (a): "; cin >> sideA; cout << "Enter the length of the second side (b): "; cin >> sideB; cout << "Enter the length of the third side (c - the largest side): "; cin >> sideC; if (pow(sideC, 2) == pow(sideA, 2) + pow(sideB, 2)) { cout << "The given sides form a right triangle." << endl; } else { cout << "The given sides do not form a right triangle." << endl; } } else { cout << "Invalid choice!" << endl; } return 0; }
Output
The program provides two functionalities, with examples of each:
Example 1: Calculating the Hypotenuse
Choose an option: 1. Calculate the hypotenuse 2. Verify if three sides form a right triangle 1 Enter the length of the first side (a): 3 Enter the length of the second side (b): 4 The length of the hypotenuse is: 5
Example 2: Verifying a Right Triangle
Choose an option: 1. Calculate the hypotenuse 2. Verify if three sides form a right triangle 2 Enter the length of the first side (a): 6 Enter the length of the second side (b): 8 Enter the length of the third side (c - the largest side): 10 The given sides form a right triangle.
Explanation
The program is divided into two parts:
-
Hypotenuse Calculation:
- The user provides the lengths of two sides of a right triangle.
- The program computes the hypotenuse using the formula
c = sqrt(a² + b²)
. - The
sqrt
andpow
functions from thecmath
library are used for mathematical calculations.
-
Right Triangle Verification:
- The user provides the lengths of all three sides of a triangle.
- The program checks if the Pythagorean Theorem holds true for the given sides (i.e.,
c² = a² + b²
). - If the condition is met, the triangle is classified as a right triangle; otherwise, it is not.
This program demonstrates a practical application of the Pythagorean Theorem in solving mathematical problems and validating geometric shapes.