ش | ی | د | س | چ | پ | ج |
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
به زبان سیپلاسپلاس و با بهکارگیری الگوریتم بازگشتی، برنامهای بنویسید که پیوسته مقسومعلیه دو عدد طبیعی داده شده را محاسبه کند. در صورتی که کاربر عددی کوچکتر از یک را وارد کند، خروج از برنامه رخ میدهد.
Using a recursive algorithm, write a program in C++ which continuously calculates the greatest common divisor of two given natural numbers. If either of the two numbers is less than 1, the program is terminated.
#include <iostream>
using namespace std;
int gcd(int a, int b)
{
if(b==0)
{
return a;
}
else
{
return gcd(b, a % b);
}
}
int main()
{
cout << "The Greatest Common Divisor of Two Natural Numbers \n\n";
cout << "Programmer: Mohammad Rajabpur \t rajabpur.blogsky.com \n\n";
cout << "To exit the program, enter a number less than 1 \n\n";
int x, y;
while(true)
{
cout << " x = ";
cin >> x;
cout << " y = ";
cin >> y;
if (x < 1 || y < 1)
{
cout << "The program is terminated.";
break;
}
cout << "gcd = " << gcd(x, y) << endl << endl;
}
}