I have written the below code to calculate the GCD and LCM.
It works fine for the given set of inputs in the question as well as for some other inputs which i tried.
If when running on codechef it says Wrong Answer.
Not sure what is causing this error.
Any suggestion would be helpful.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class GCDandLCM
{
public static void main(String[] args) throws Exception
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
PrintWriter pw = new PrintWriter(System.out);
int testcases = Integer.parseInt(br.readLine());
int gcd = 0;
while(testcases-- > 0)
{
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
for (int i = 1; i <= a; i++)
{
if(a % i == 0 && b % i == 0)
gcd = i;
}
pw.println(gcd + " " + a * b / gcd);
}
pw.flush();
}
}