بزرگ ترین مقسوم علیه مشترک(GCD) دو عدد صحیح عبارت است از بزرگ ترین عدد صحیحی که هر دو عدد بر ان بخش پذیر باشند. تابعی به نام gcd بنویسید که بزرگ ترین مقسوم علیه مشترک دو عدد را برگرداند.
#include<iostream>
using std::cout;
using std::cin;
int gcd(int, int);
int main()
{
int a, b;
for(int j=1; j=5; ++j) {
cout << "Enter two integers: ";
cin >> a >> b;
cout << "The greatest common divisor of " << a << " and "
<< b << " is " << gcd(a, b) << "\n\n";
}
return 0;
}
//+_+_+_+_+_+_+_+_+_+
int gcd (int x, int y)
{
int greatest = 1;
for(int i=2; i <= ((x < y) ? x: y); ++i)
if(x % i == 0 && y % i == 0)
greatest = i;
return greatest;
}