// // Program: triangle // // Purpose: decide whether three positive integers could form the // sides of a right triangle. // Input: three positive integers, from standard input (program will // prompt for them), in non-decreasing order. // Output: message indicating whether they could form the sides of // a right triangle, based on Pythagorean theorem (or an error // message, if the numbers are not positive and in non-decreasing // order. // #include #include // has EXIT_SUCCESS, EXIT_FAILURE // Prompt for an integer, with a minimum acceptable value. // Pre: none. // Post: user has been prompted to enter an integer >= "min". // if response is >= "min", it is returned; otherwise the // program is exited. int get_at_least(int min); int main(void) { cout << "This program determines whether three numbers form " << "the sides of a right triangle.\n"; // First number must be at least 1. int x = get_at_least(1); // Second number must be at least as big as first. int y = get_at_least(x); // Third number must be at least as big as second. int z = get_at_least(y); // Echo input. cout << x << ", " << y << ", and " << z; // Check whether x, y, z form right triangle (using Pythagorean // theorem) and print. if ((x*x + y*y) == z*z) cout << " form a right triangle.\n"; else cout << " do not form a right triangle.\n"; return EXIT_SUCCESS; } int get_at_least(int min) { int temp; cout << "Enter an integer no less than " << min << ": "; cin >> temp; if (temp < min) { cout << "Error! " << temp << " is less than " << min << endl; exit (EXIT_FAILURE); } return temp; }