+ 3

Look this c++ code.

https://code.sololearn.com/cVAE0HBy6QJg/?ref=app It's working properly till 12! but after that it's not. Why? As per me there is no logical error in it. if you find one, please tell.

29th Dec 2017, 1:17 AM
Qwqwq Rt
Qwqwq Rt - avatar
12 Answers
+ 6
@Rame you have to increase memory size of your variable and not function. keep int function() change unsigned long long int y;
29th Dec 2017, 12:40 PM
Dhruv garg
Dhruv garg - avatar
+ 6
no don't change function datatype, it won't have any affect. you need to increase storage memory of int variable by using, unsigned long long int y; where y is your variable storing value of factorial.
29th Dec 2017, 1:45 PM
Dhruv garg
Dhruv garg - avatar
+ 6
@rame i am not really sure about that, but you can try both cases.
30th Dec 2017, 3:37 AM
Dhruv garg
Dhruv garg - avatar
+ 4
You hit the maximum value int can store. Replace 'int' with 'long long' and it will go further. However, you will hit that maximum after a few more numbers.
29th Dec 2017, 1:32 AM
John Wells
John Wells - avatar
+ 4
There must be some corrections in your code https://code.sololearn.com/cmPHgzp3RAnV/?ref=app
29th Dec 2017, 2:10 PM
Abhishek Tandon
Abhishek Tandon - avatar
+ 3
@john wells #include <iostream> using namespace std; long long fact(long long x) { long long y,i,prod=1; cin>>y; for(i=1;i<=y;i++) { prod*=i; } return prod; } int main() { long long y; cout<<fact(y); return 0; } is this correct?
29th Dec 2017, 1:46 AM
Qwqwq Rt
Qwqwq Rt - avatar
+ 3
Please check it.
29th Dec 2017, 1:47 AM
Qwqwq Rt
Qwqwq Rt - avatar
+ 3
@infinity The code you posted is public? I guess you forgot to do so.
30th Dec 2017, 3:40 AM
Qwqwq Rt
Qwqwq Rt - avatar
+ 2
@dhruv garg do i need to change my Function datatype even when I use use recursion( like one I used in my program) ?
29th Dec 2017, 1:09 PM
Qwqwq Rt
Qwqwq Rt - avatar