GCD and LCM Codechef Solution

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

GCD and LCM Codechef Solution
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.


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


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


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.


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



120 140
10213 312
10 30


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
        a,b = b,a%b
    hcf = a
    lcm = multiply/hcf

GCD and LCM CodeChef Solutions in CPP

#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    // your code goes here
    int t=0;
        long long int a=0,b=0;
        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) {
            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.

Next: Puppy and Sum Codechef Solution

Leave a Reply

Your email address will not be published. Required fields are marked *