C++ UNCLEAR CHALLENGE QUESTION πŸ™‹πŸΎβ€β™‚οΈ | Sololearn: Learn to code for FREE!

+6

C++ UNCLEAR CHALLENGE QUESTION πŸ™‹πŸΎβ€β™‚οΈ

https://www.sololearn.com/post/1691178/?ref=app Fill in the blanks to get a function calculating factorial. 1 int main() { 2 int fact(int n){ 3 if(n __= 0) 4 return 1; 5 return __*fact(__ - 1); 6 } The correct item in line 3 according to sololearn is '=='; but I think '<=' is more appropriate. What do you think?

5/27/2022 10:52:49 AM

πŸ‘‘ Tchybooxuur!

6 Answers

New Answer

+7

if there is "<=" used then if condition will satisfied and it will return 1 everytime and function call is back to callee function... which is not correct we have to calculate factorial and for that we have to execute the second return statement. And that is the reason. i think this is the reason if i am wrong then please correct

+4

πŸ‘‘ Tchybooxuur! As I remember in the SL course it was also example with β€œ==β€œ. But I might be wrongπŸ˜ƒ

+4

with this only the missing thing is: Please enter a positive integer 🦁

+2

Pallavi Jha You are right βœ”οΈβœ”οΈ πŸ’― if you use "<=", it will run only one time. Going inside the if statement will end the recursion immediately. So it will return "n" as the final answer it will be "==" not "<=" you can also write, πŸ‘‡ if ( n == 1 ) return 1; because multiplication with 1 gives the same answer. this way there is No need for one extra calling .

+2

Thanks for the input guys πŸ‘πŸΌ

-3