LCM And HCF
PROBLEM :
For 2 given numbers find out their LCM and HCF.
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
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include <iostream>
using namespace std;
int hcf(int ,int ) ;
int lcm(int ,int ) ;
int gcd(int ,int ) ;
int main() {
int t,a,b ;
cin>>t ;
while(t--)
{
cin>>a>>b ;
cout<<lcm(a,b)<<" "<<hcf(a,b) ;
cout<<endl ;
}
return 0;
}
int hcf(int a,int b)
{
return a>b?gcd(b,a):gcd(a,b) ;
}
int lcm(int a,int b)
{
return a*b/(a>b?gcd(b,a):gcd(a,b)) ;
}
int gcd(int a,int b)
{
int rem ;
rem=b%a ;
if(rem==0)
return a ;
else
return gcd(rem,a) ;
}
---------------------------------------------------------------------------------
For 2 given numbers find out their LCM and HCF.
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
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include <iostream>
using namespace std;
int hcf(int ,int ) ;
int lcm(int ,int ) ;
int gcd(int ,int ) ;
int main() {
int t,a,b ;
cin>>t ;
while(t--)
{
cin>>a>>b ;
cout<<lcm(a,b)<<" "<<hcf(a,b) ;
cout<<endl ;
}
return 0;
}
int hcf(int a,int b)
{
return a>b?gcd(b,a):gcd(a,b) ;
}
int lcm(int a,int b)
{
return a*b/(a>b?gcd(b,a):gcd(a,b)) ;
}
int gcd(int a,int b)
{
int rem ;
rem=b%a ;
if(rem==0)
return a ;
else
return gcd(rem,a) ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment