Inaccurate function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Inaccurate function

The factorial of 0 is 1, its a small detail but the code doesnt account for it

12th Jan 2017, 12:07 AM
Mokkants
3 Answers
+ 4
It is hard to answer because you didn't mention your code. Anyways, this mistake is possible. int factorial (int num) { if (num ==1) return 1; else return n * factorial(n - 1); } I think that you used the above code for factorial function. The least number for which it works is 1 because of the condition (num == 1). If you replace it with (num <= 1), the function works for 0 too.
15th Jan 2017, 9:47 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 3
show your code
12th Jan 2017, 7:33 PM
mojtaba noghabaee
mojtaba noghabaee - avatar
0
oh i meant this as an answer to the example code, im not quite sure how this discussions section works heh
15th Jan 2017, 6:50 PM
Mokkants