diff options
Diffstat (limited to 'Q3a.cpp')
-rw-r--r-- | Q3a.cpp | 36 |
1 files changed, 36 insertions, 0 deletions
@@ -0,0 +1,36 @@ + +#include <iostream> + +using namespace std; + +int gcd(int m, int n); + +int gcd(int m, int n) +{ + if (m <= 0 || n <= 0) + return 0; + + if (m % n == 0) + return n; + else + return gcd(n, m%n); +} + +int main(int argc, const char *argv[]) +{ + int m, n; + int result; + cout << "Enter 2 numbers to calculate the greatest common divisor (0 to quit)" << endl; + do { + cout << ">> "; + cin >> m >> n; + if (m <= 0 || n <= 0) { + cout << "Please input positive numbers (0 to quit)" << endl; + } else { + result = gcd(m, n); + cout << "gcd(" << m << ", " << n << ") = " << result << endl; + } + } while (m != 0 || n != 0); + + return 0; +} |