Problem no 10: LCM and GCD.

 QUESTION:

LCM And GCD


Given two numbers A and B. The task is to find out their LCM and GCD.

 

Example 1:

Input:
A = 5 , B = 10
Output:
10 5
Explanation:
LCM of 5 and 10 is 10, while
thier GCD is 5.

Example 1:

Input:
A = 14 , B = 8
Output:
56 2
Explanation:
LCM of 14 and 8 is 56, while
thier GCD is 2.

 

ANSWER:


    vector<long long> lcmAndGcd(long long a , long long b) {
        vector<long long> vec;
         long long lcmm=lcm(a,b);   ......   CALLING LCM FUNCTION.
         vec.push_back(lcmm);
   long long c=gcd(a,b);                ......   CALLING GCD FUNCTION.
   vec.push_back(c);
   return vec;
    }

 

 GCD FUNCTION IS GIVEN BELOW:

long long  gcd(long long a, long long b)
    {
        if (a == 0)
        return b;
    return gcd(b % a, a);
         
    }  

 

LCM FUNCTION IS GIVEN BELOW:

 long long lcm(long long a,long long b){    
    if(a>b)
        return (a/gcd(a,b))*b;
    else
        return (b/gcd(a,b))*a;   
}

Comments

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

Problem no 16: count the number of squares below N.