Basics of Programming

Recursive and Iterative

Solving problems with iterative and recursive methods are two quite different approaches, somehow, to the same kind of problems.

Here we will calculate the factorial of $n$. We define two functions using the iterative method and the recursive method.

Run the program on

def recursiveFactorial(n):
  if n == 0:
    return 1
    return n * recursiveFactorial(n - 1)

def iterativeFactorial(n):
  ans = 1


  while i <= n:
    ans = ans * i

  return ans


Planted: by ;

No backlinks identified. Reference this note using the Note ID wiki/computation/ in other notes to connect them.

L Ma (2018). 'Basics of Programming', Datumorphism, 09 April. Available at: