رایانشکده *** Computation Hub

رایانشکده *** Computation Hub

محاسبات ریاضی، حل مسئله های برنامه نویسی و پیاده سازی الگوریتم
رایانشکده *** Computation Hub

رایانشکده *** Computation Hub

محاسبات ریاضی، حل مسئله های برنامه نویسی و پیاده سازی الگوریتم

ب.م.م. دو عدد با الگوریتم بازگشتی در سی‌پلاس‌پلاس

به زبان سی‌پلاس‌پلاس و با به‌کارگیری الگوریتم بازگشتی، برنامه‌ای بنویسید که پیوسته مقسوم‌علیه دو عدد طبیعی داده شده را محاسبه کند. در صورتی که کاربر عددی کوچکتر از یک را وارد کند، خروج از برنامه رخ می‌دهد.

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;                   

            }                     

}




دانلود کد برنامه

نظرات 0 + ارسال نظر
برای نمایش آواتار خود در این وبلاگ در سایت Gravatar.com ثبت نام کنید. (راهنما)
ایمیل شما بعد از ثبت نمایش داده نخواهد شد