10 Simple Recursion Programs in Python
What is Recursion? Recursion is a technique where a function calls itself to solve a smaller version of the same problem. Every recursive function must have: Base Case → Stops the recursion Recursi...

Source: DEV Community
What is Recursion? Recursion is a technique where a function calls itself to solve a smaller version of the same problem. Every recursive function must have: Base Case → Stops the recursion Recursive Case → Calls the function again with a smaller input 1. Factorial of a Number The factorial of a number n is the product of all positive integers up to n. def factorial(n): if n == 0 or n == 1: return 1 return n * factorial(n - 1) print(factorial(5)) 2. Sum of First N Numbers This program calculates the sum of numbers from 1 to n. def sum_n(n): if n == 0: return 0 return n + sum_n(n - 1) print(sum_n(5)) 3. Print Numbers from 1 to N Print numbers in ascending order using recursion. def print_1_to_n(n): if n == 0: return print_1_to_n(n - 1) print(n) print_1_to_n(5) 4. Print Numbers from N to 1 Print numbers in descending order. def print_n_to_1(n): if n == 0: return print(n) print_n_to_1(n - 1) print_n_to_1(5) 5. Fibonacci Series (Nth Term) Each number is the sum of the previous two. def fib