0

[SOLVED]Why this c++ code isn't working properly for n>13?

This program works fine for n<13. It does wrong division for p. (Eg for n=13 and r=4) https://code.sololearn.com/cPw82NKfkJwn/?ref=app #include <iostream> using namespace std; int factorial(int a) { long long int s=1; for(int q=1;q<=a;q++) { s=s*q;} cout<<s<<endl; return s; } int main() { long long int n,r; long long int p,q,s; cin>>n>>r; q=factorial(n),s=factorial(n-r); p=q/s; cout<<p; return 0; } But if you try to do the division manually then it gives correct output. https://code.sololearn.com/cILguJHQc37d/?ref=app #include <iostream> using namespace std; int main() {long long int i=6227020800; //13! long long int j=362880; //13-4 ie 9! long long int k=i/j; cout<<k; return 0; }

5th Jan 2021, 4:11 PM
Yugal Kishore
1 Odpowiedź
+ 1
Oh I got it, I've to use long int factorial (int a) Instead of int factorial (int a)
5th Jan 2021, 5:01 PM
Yugal Kishore