Finding LCM

Normal iterative way:

#include <iostream>

using namespace std;

int main()
{
    int a,b,x,i,lcm;
    cout<<"Enter one value: "<<endl;
    cin>>a;
    cout<<"Enter another value: ";
    cin>>b;
    x=max(a,b);
    for(i=x;;i++)
    {
        if(i%a==0 && i%b==0)
        {
            lcm=i;
            break;
        }
    }
    cout<<"LCM is "<<lcm<<endl;

    return 0;
}

Using GCD:
This way has a chance of overflow.

#include <iostream>

using namespace std;

int gcd(int a,int b)
{
    return b==0 ? a : gcd(b,a%b);
}

int main()
{
    int c,d,lcm;
     for(;;)
    {
        cout<<"Enter any number:"<<endl;
        cin>>c;
        cout<<"Enter another number:"<<endl;
        cin>>d;

        lcm=(c*d)/gcd(c,d);

        cout<<"LCM is "<<gcd(c,d)<<endl<<endl;
    }
    return 0;
}

Using GCD:
This way hasn’t a chance of overflow. It’s the best way.

#include <iostream>

using namespace std;

int gcd(int a,int b)
{
    return b==0 ? a : gcd(b,a%b);
}

int main()
{
    int c,d,lcm;
     for(;;)
    {
        cout<<"Enter any number:"<<endl;
        cin>>c;
        cout<<"Enter another number:"<<endl;
        cin>>d;

        lcm=(c*d)/gcd(c,d);

        cout<<"LCM is "<<gcd(c,d)<<endl<<endl;
    }
    return 0;
}

Finding GCD

Iterative way:

#include <iostream>

using namespace std;

int main()
{
    int a,b,x,gcd;
    cout<<"Enter one value: "<<endl;
    cin>>a;
    cout<<"Enter another value: ";
    cin>>b;
    if(a<b)
    {
        x=a;
    }
    else
    {
        x=b;
    }
    for(;x>=1;x--)
    {
        if(a%x==0 && b%x==0)
        {
            gcd=x;
            break;
        }
    }
    cout<<"GCD is "<<gcd<<endl;


    return 0;
}

 

Euclidian algorithm ( Recursive way):

#include <iostream>

using namespace std;


int main()
{
    int a,b,remainder,gcd;
    cout<<"Enter one value: "<<endl;
    cin>>a;
    cout<<"Enter another value: "<<endl;
    cin>>b;

    // Assuming a>b always

    while(remainder!=0)
    {
        remainder=a%b;
        if(remainder==0)
        {
            gcd=b;
            cout<<"GCD is "<<gcd<<endl;
        }
        a=b;
        b=remainder;
    }
    return 0;
}

The above code can be written further in a shortcut as follows:

#include <iostream>

using namespace std;

int gcd(int a,int b)
{
    return b==0 ? a : gcd(b,a%b);
}

int main()
{
    int c,d;

    for(;;)
    {
        cout<<"Enter any number:"<<endl;
        cin>>c;
        cout<<"Enter another number:"<<endl;
        cin>>d;

        cout<<"GCD is "<<gcd(c,d)<<endl<<endl;
    }


    return 0;
}

UVa – 11889 – Benefit

#include <bits/stdc++.h>

#define pf                  printf
#define sf(a)               scanf("%d",&a)
#define sfl(a)              scanf("%lld",&a)
#define sff(a,b)            scanf("%d %d",&a,&b)
#define sffl(a,b)           scanf("%lld %lld",&a,&b)
#define sfff(a,b,c)         scanf("%d %d %d",&a,&b,&c)
#define sfffl(a,b,c)        scanf("%lld %lld %lld",&a,&b,&c)
#define sffff(a,b,c,d)      scanf("%d %d %d %d",&a,&b,&c,&d)
#define sffffl(a,b,c,d)     scanf("%lld %lld %lld %lld",&a,&b,&c,&d)
#define sfffff(a,b,c,d,e)   scanf("%d %d %d %d %d",&a,&b,&c,&d,&e)
#define sfffffl(a,b,c,d,e)  scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&e)
#define sfc(a)              scanf("%c",&a)
#define pii                 pair<int,int>
#define ms(a,b)             memset(a,b,sizeof(a))
#define pb(a)               push_back(a)
#define pbp(a,b)            push_back({a,b})
#define db                  double
#define ft                  float
#define ll                  long long
#define ull                 unsigned long long
#define pii                 pair<int,int>
#define ff                  first
#define ss                  second
#define sz(x)               x.size()
#define all(x)              x.begin(),x.end()
#define CIN                 ios_base::sync_with_stdio(0); cin.tie(0)
#define max3(a, b, c)       max(a, b) > max(b, c) ? max(a, b) : max(b, c)
#define min3(a, b, c)       min(a, b) < min(b, c) ? min(a, b) : min(b, c)
#define for0(i,n)           for(int i=0;i<n;i++)
#define for1(i,n)           for(int i=1;i<=n;i++)
#define forrev(i,n)         for(int i=n-1; i>=0; i--)
#define forab(i,a,b)        for(int i=a;i<=b;i++)
#define forba(i,b,a)        for(int i=b;i>=a;i--)
#define stlloop(x)          for(__typeof(x.begin()) it=x.begin();it!=x.end();it++)
#define gcd(a, b)           __gcd(a, b)
#define lcm(a, b)           ((a)*((b)/gcd(a,b)))
#define case1(z)            cout<<"Case "<<z<<": "
#define case2(z)            printf("Case %d: ",z)
#define PI                  acos(-1) //3.14159265358979323846264338328
#define valid(tx,ty)        tx>=0 && tx<row && ty>=0 && ty<col
#define intlim              2147483648
#define MAX                 10000007
#define inf                 100000008

/*------------------------------Graph Moves----------------------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1};   // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1};  // Kings Move
//const int fx[]={-2, -2, -1, -1,  1,  1,  2,  2};  // Knights Move
//const int fy[]={-1,  1, -2,  2, -2,  2, -1,  1}; // Knights Move
/*---------------------------------------------------------------------*/

using namespace std;

int main()
{
    //CIN;
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    int t,a,c,b;
    sf(t);
    for1(z,t)
    {
        sff(a,c);
        if(c%a==0)
        {
            int b=c/a,i;
            for(i=b;i<=MAX;i+=b)
            {
                if(lcm(a,i)==c) break;
            }
            pf("%d\n",i);
        }
        else pf("NO SOLUTION\n");
    }

    return 0;
}