The greatest common divisor and the least common multiple

Hello! Today I am going to show you another basic algorithm. one that finds out the greatest common divisor and the least common multiple of two numbers using repetitive subtractions. Enjoy!

#include 

using namespace std;

int main()
{
    int a,b,auxa,auxb, m;
    cout<<"a=";cin>>a;
    cout<<"b=";cin>>b;
    auxa=a;
    auxb=b;
    
    while (a!=b)
    {
      if (a>b)
        a=a-b;
      else
        b=b-a;
      }
    m=auxa*auxb/a;
    cout<<"The greatest common divisor is "<<a<<endl;
    cout<<"The least common multiple "<<m<<endl;
    return 0;
}

I hope this helped you! If you have any questions feel free to ask me!

Keep on coding 😉

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s