TechoSagar: LCM And GCD

Search

Google Alert - jobs

LCM And GCD

For 2 given numbers find out their LCM and GCD.
Input:
The first line contains an integer 'T' denoting the total number of test cases. In each test cases, there are two numbers a and b.

Output:
Find LCM and HCF. 

Constraints:
1 <= T <= 30
1 <= a <= 1000
1 <= b <= 1000

Example:
Input:
2
5 10
14 8
Output:
10 5
56 2
Your Code
#include<iostream>

using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
int a, b,x,y;
cin>>a>>b;
x=a;
y=b;
int i=1, j=1;
while(x!=y)
{  if(x<y)
{
x=a*i;
i++;
   }
   if(y<x)
   {
    y=b*j;
    j++;
   
}
}
int gcd;
gcd=(a*b)/y;
cout<<y<<" "<<gcd<<endl;
}
}

Follow us

Follow Bijendra Kumar on Facebook