Write a program that enable one to key in any 4 numbers and determine their GCD.
#include <iostream>
int gcd(int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a-b, b);
if (a < b)
return gcd(a, b-a);
}
int main()
{
int a, b, c, d;
std::cout << "Enter any positive four numbers:" << std::endl;
std::cin >> a >> b >> c >> d;
std::cout << "GCD(" << a << ", " << b << ", " << c << ", " << d <<") = " << gcd(gcd(a, b), gcd(c, d)) << std::endl;)
return 0;
}
Comments
Leave a comment