| 1. |
Differentiate between recursion and iteration. |
|
Answer» Recursion and iteration: The factorial of a number, written as n!, can be calculated as follows: n! = n * (n-1) * (n-2) * (n-3) * ... * (2) * 1 This approach is called iteration, that is step by step calculation. However, we can also calculate n! like: n! = n * (n-1)! Going one step further, we can calculate (n-1)! using the same procedure: (n-1)! = (n-1) * (n-2)! We can continue calculating recursively until the value of n is 1. This approach is called recursion. It refers to a situation in which a function calls itself either directly or indirectly. Indirect recursion occurs when one function calls another function that then calls the first function. |
|