C++ UNCLEAR CHALLENGE QUESTION 🙋🏾‍♂️ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

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?

27th May 2022, 10:52 AM
👑 Tchybooxuur!
👑 Tchybooxuur! - avatar
6 Answers
+ 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
27th May 2022, 11:24 AM
Pallavi Jha
Pallavi Jha - avatar
+ 5
with this only the missing thing is: Please enter a positive integer 🦁
27th May 2022, 2:31 PM
JOY
JOY - avatar
+ 4
👑 Tchybooxuur! As I remember in the SL course it was also example with “==“. But I might be wrong😃
27th May 2022, 11:16 AM
Mikhail
Mikhail - avatar
+ 3
Thanks for the input guys 👍🏼
27th May 2022, 5:16 PM
👑 Tchybooxuur!
👑 Tchybooxuur! - avatar
+ 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 .
27th May 2022, 3:12 PM
NonStop CODING
NonStop CODING - avatar
- 3
.
28th May 2022, 6:31 PM
Lura~🐡