# GCD and LCM Codechef Solution

#### ByBrokenprogrammers

Dec 7, 2022

Hello Programmers In this post, you will know how to solve the GCD and LCM Codechef Solution.

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.

### Problem

Two integers A and B are the inputs. Write a program to find GCD and LCM of A and B.

### Input

The first line contains an integer T, total number of testcases. Then follow T lines, each line contains an integer A and B.

### Output

Display the GCD and LCM of A and B separated by space respectively. The answer for each test case must be displayed in a new line.

### Constraints

• 1 <= T <= 1000
• 1 <= A, B <= 100000

### Example

Input:

```3
120 140
10213 312
10 30
```

Output:

```20 840
1 3186456
10 30```

### GCD and LCM CodeChef Solutions in Python

```T = int(input())
for i in range(T):
num = [int(x) for x in input().split(' ')]
a = min(num)
b = max(num)
multiply = a*b
while(b):
a,b = b,a%b
hcf = a
lcm = multiply/hcf
print(hcf,int(lcm))```

### GCD and LCM CodeChef Solutions in CPP

```#include <iostream>
#include <algorithm>
using namespace std;
int main() {
// your code goes here
int t=0;
cin>>t;
while(t--)
{
long long int a=0,b=0;
cin>>a>>b;
cout<<__gcd(a,b)<<" "<<((a*b)/__gcd(a,b))<<endl;
}
return 0;
}```

### GCD and LCM CodeChef Solutions in JAVA

```import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
int t=input.nextInt();
long a,b,Gcd,Lcm;
while(t-->0) {
a=input.nextInt();
b=input.nextInt();
Gcd=getGcd(a,b);
Lcm=(a*b)/Gcd;
System.out.println(Gcd+" "+Lcm);
}
}
public static long getGcd(long a, long b) {
while(a != b) {
if(a>b) a=a-b;
else b=b-a;
}
return a;
}
}```

Disclaimer: The above Problem (GCD and LCM) is generated by CodeChef but the solution is provided by BrokenProgrammers. This tutorial is only for Educational and Learning purpose.

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.