Gcd of lcm of two number (FLOW016)

why i am getting the wrong answer

using namespace std;

int gcd(int a,int b){
return a;

return gcd(b,a%b);


int lcm(int a,int b){
return a*b/gcd(a,b);

int main(){
int t;

    int a,b;
    cout<<gcd(a,b)<<" "<<lcm(a,b)<<"\n";
return 0;


You didn’t link the problem, so I’m assuming that it’s not an overflow issue because I can’t see the constraints.

a*b should be in parenthesis. Also, I like to use the inbuilt __gcd(a, b) function rather than making my own.
My code:

#include <bits/stdc++.h>

using namespace std;

int main()
    int t;
    cin >> t;
    while (t--)
        long long a, b;
        cin >> a >> b;
        cout << __gcd(a, b) << " " << (a*b)/__gcd(a, b) << '\n';

make all this long int

just modified your code .

Should be :

long long lcm(int a,int b){
return a*b/gcd(a,b);

yes I got that…
Thanks for reply…

1 Like

there is inbuild function for GCD…
I don,t know that…
I learn something new…
thanks bade bhaiya… :innocent: