Pythagorean triplet (0-200)

10:21 0 Comments A+ a-

Problem :
A right triangle can have sides that are all integers. A set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal to the square of the hypotenuse. Find and display all Pythagorean triples for side1, side2 and hypotenuse all no larger than 200. (Hint: Use a triple-nested for loop that tries all possibilities.)
Solution:
# include <iostream>
# include <cmath>
using namespace std;
int main()
{
for(int hyp=1;hyp<=200;hyp++) //loop for hypotenuse started from 0-200
{
for(int perp=1;perp<hyp;perp++) //loop for perpendicular started to test each value less than hypotenuse
{
for(int base=1;base<hyp;base++) //loop for base started to check each value less than hypotenuse
{

if(pow(hyp,2)==pow(perp,2)+pow(base,2))  //Condition of Pythagorean Theorem Applied
{
cout<<"Pythagorean Triplet is " //display pythagorean triplet which satisfy pythagorean theorem
<<"Hypotenuse = "<<hyp //display Hypotenuse of pythagorean triplet
<<", Perpendicular = "<<perp //display Perpendicular of pythagorean triplet
<<", Base = "<<base<<endl; //display Base of pythagorean triplet
}
}
}
}



system("pause");
return 0;

}