+ 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?
6 Antworten
+ 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
+ 5
with this only the missing thing is: Please enter a positive integer 🦁
+ 4
👑 Tchybooxuur! As I remember in the SL course it was also example with “==“. But I might be wrong😃
+ 3
Thanks for the input guys 👍🏼
+ 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 .
- 3
.