Lab04 - Pythagorean Triple

About this Page

User enters m and n - two positive integers, where m > n and both m and n are greater than zero. Using m and n, the lengths of three sides of a right triangle (consisting of integer-length sides) are calculated. The pythagorean triplet calculated in the processing step is printed on the user's screen.

/* Title     - Pythagorean Triple
 * File name - Lab04
 * Programmer- 415 Erich Musick
 * IPO       - Input - User enters m and n - two positive integers, where m > n
                and both m and n are greater than zero
               Processing - Using m and n, the lengths of three sides of a right
                triangle (consisting of integer-length sides) are calculated.
               Output - The pythagorean triplet calculated in the processing
                step is printed on the user's screen.
*/

#include 

using namespace std;

int main() {
        int a;
        int b;
        int c;
        int m;
        int n;

        cout << "Enter the larger of two integers greater than zero:\nm? ";
        cin >> m;

        cout << "\nEnter the lesser of the integers (must be > 0 and not equal to m):\nn? ";
        cin >> n;

        if (m <= n || m <= 0 || n <= 0) {
                cout << "\nError: n must be less than m and both m and n must be greater than zero\n";
        }
        else {
                a = m*m - n*n;
                b = 2*m*n;
                c = m*m + n*n;
                cout << "\nPythagorean triple: a = " << a;
                cout << ", b = " << b;
                cout << ", c = " << c;
                cout << endl;
        }

        return 0;
}

Return to C++ Snippetts