Write a program to key in any four numbers and determine the G.C.D.
#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