/* ************************************************************************** * Program name : 020_Pythagorean_Triples (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2000/06/11 - first version * ************************************************************************** * Description : (a) Input the maximum length of longest size * * (b) Output all the possible soultions * * (c) Try again ? * ************************************************************************** */ #include #include int main() { // part 1 : declaration int Number, Size_A, Size_B, Size_C, Counter; char again; do { // part 2 : Repeat asking the maximum length of the longest size do { cout << "\nThis program will try to list the length of" << "\neach sizes of a Pythagorean Triples." << "\nPlease input maximum length of the longest size : "; cin >> Number; if (Number<=0) cout << "The number must be greater than zero (o)\n"; cout << "\n"; } while (Number<=0); // part 3 : Counter = 0; cout << "\n\t\tSize A\tSize B\tSize c" << "\n\t\t------\t------\t------"; for (Size_A=Number; Size_A>=1; Size_A--) // Note (1) for (Size_B=(Size_A-1); Size_B>=1; Size_B--) for (Size_C=(Size_B-1); Size_C>=1; Size_C--) if (Size_A*Size_A==(Size_B*Size_B+Size_C*Size_C)) // Note (2) { cout << "\n\t\t " << Size_A << "\t " << Size_B << "\t " << Size_C; Counter +=1 ; } cout << "\n\t\t------\t------\t------" << "\n\t\tThere is (are) " << Counter << " solution(s)." << endl; // part 4 : try another number ? cout << "\n\t\aTry again (Y/N) : "; cin >> again; cout << "\n"; } while (again=='Y' || again=='y'); cout << "\n" << endl; system("PAUSE"); return 0; } /* Notes (1) Let's say the shortest size of the Pythagorean Triples is Size_C. The longer size is Size_B and the longest size is Size_A. (2) When the square of Size_A equals to the square of Size_B plus square of Size_C, that is a Pythagorean Triples. */