Gcd of lcm of two number (FLOW016)

why i am getting the wrong answer

#include<bits/stdc++.h>
using namespace std;

int gcd(int a,int b){
if(b==0)
return a;

return gcd(b,a%b);

}

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

int main(){
int t;
cin>>t;

while(t--){
    int a,b;
    cin>>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

https://www.codechef.com/viewsolution/35618514
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:

:grin: