+ 1
Help me find my mistake
I don't know why, but it doesn't show the final value of the mathematicial example. Help me, please: https://code.sololearn.com/co65U1LAMyc0/#cpp
6 Answers
+ 2
In your code, factorial(0) is not defined, so put if(q<=1) instead of if(q==1) inside the factorial function to define it. And also, the iterator in the for loop goes until n, not n-1, so change k<n to k<=n or k<n+1(i recommend the first) in the condition of the for loop.
+ 6
if (q==0 || q==1)
return 1;
else return q*factorial(q-1);
Factorial of 0 is also 1. Change your function as above to consider 0 (since k starts from 0)
+ 6
Khidoyat Tashmatov The value held by uninitialised local variables will vary from compiler to compiler. It is highly encouraged to initialize z to 0, knowing what you are doing. VS for instance, will warn you about using an uninitialised local variable.
https://stackoverflow.com/questions/6032638/default-variable-value
+ 1
Shamima Yasmin, thank you very much! you helped me again :)
+ 1
Bebida Roja now it definitely works, thank you!
0
Hatsy Rei, by default, z equals to zero