Monday, November 13, 2017

GCD and LCM Codechef Problem Solution


https://www.codechef.com/problems/FLOW016


In C++......................

#include <iostream>

using namespace std;

long long gcd(long long  a, long long b)
{
    // Everything divides 0
    if (a == 0 || b == 0)
       return 0;

    // base case
    if (a == b)
        return a;

    // a is greater
    if (a > b)
        return gcd(a-b, b);
    return gcd(a, b-a);
}

int main()
{
   long long t,a,b;
   cin>>t;
   while(t--)
   {
       cin>>a>>b;
       cout<<gcd(a,b)<<" "<<(a*b)/gcd(a,b)<<endl;
   }
   return 0;
}

No comments:

Post a Comment