Problem Description
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.
Constraints
1 <= T<= 1000
1 <= A,B <= 1000000Test Case 1
Input (stdin)5 2 3 2 4 3 5 4 6 7 8
Expected Output1 6 2 4 1 15 2 12 1 56
Test Case 2
Input (stdin)3 120 11 10213 312 10 3
Expected Output1 1320 1 3186456 1 30
Program
#include <stdio.h> int main() { int m,a,b,n,d,r,gcd,lcm; scanf("%d",&m); while(m--) { scanf("%d %d",&a,&b); if(a>b) { n=a; d=b; } else { d=a; n=b; } r=n%d; while(r!=0) { n=d; d=r; r=n%d; } gcd=d; lcm=(a*b)/gcd; printf("%d %d\n",gcd,lcm); } return 0; }
No comments:
Post a Comment
Note: only a member of this blog may post a comment.