Recursion wierdness? | Sololearn: Learn to code for FREE!

0

Recursion wierdness?

Already having some trouble with a factorial recursion but noticed this bit- where I try to print something and the output is not what I expected? Is it something to do with printing within a recursion function and calling local variables? It looks sort of right but the numbers only make sense in each loop going down one line. https://code.sololearn.com/cEbYqsSIAkJr/?ref=app

2/28/2020 1:02:00 PM

ren[paused]

4 Answers

New Answer

+1

Your recursion works correctly: 9! = 362880 But your code prints wrong values because you forgot to put parenthesis around num-1 in line#9 (multiplication has higher precendence than addtion). It sholud be: print(num,'x',num-1,"=",num * (num-1)) it will print the product of the current and the next numbers

+1

What is your expected output? 9! => 362880 only.. Edit: Oh.. It should be num*(num-1), instead of num*num-1

+1

You do realize num * num -1 = (num * num ) - 1 don't you? I guess you want to do num * (num - 1)

0

Haha probably wasn't a good idea to work on it at 4am :) so it was operator precidence it looks like from typo