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 Answerif 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
π Tchybooxuur! As I remember in the SL course it was also example with β==β. But I might be wrongπ
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 .
Sololearn Inc.
535 Mission Street, Suite 1591Send us a message