Thursday, January 15, 2015

C++ Program for showing pythagorean triples

#include<iostream> //for Input/Output
#include<iomanip> //for Input/Output formate
using namespace std;

int main()
{
int PythaTriples=0; //variable for calculating the total number of Pythgorean triples in first 200 numbers
//appying nested for loops
for( int h=1 ; h<=200 ; h++) //using 'h' variable for hypotenuse
{
for( int b=1 ; b<=200 ; b++) //using 'b' variable for base
{
for( int p=1 ; p<=200 ; p++) //using 'p' variable for perpendicular
{
if( (h*h) == (b*b) + (p*p) ) //applying condition for pythagorean triple
{
PythaTriples+=1;
cout << "Sides " //printing the result in a proper formate
<< setw(3)
<< h
<< " , "
<< setw(3)
<< b
<< " and "
<< setw(3)
<< p
<< " form the Pythagorean Triple"
<< endl;
}
}
}
}
cout << "\nTotal Pythagorean Triples in first 200 numbers are : " //printing total number of pythagorean triples
<< PythaTriples
<< endl;

return 0;
}

No comments:

Post a Comment