🌀 Recursion¶
Prerequisites: None
Recursion is when a function calls itself. It is a powerful but tricky concept that requires a clear Base Case to prevent infinite loops.
1. Factorial using Recursion¶
Rating: Beginner | Difficulty: Easy
Calculate n! using a recursive function.
💡 Hint
Base case:if (n <=
1) return 1. Recursive case: return n * fact(n - 1). 2. Sum of Natural Numbers¶
Rating: Beginner | Difficulty: Easy
Find the sum of numbers from 1 to N using recursion.
💡 Hint
sum(n) =
n + sum(n - 1). 3. Fibonacci Sequence (Recursive)¶
Rating: Beginner | Difficulty: Easy Find the Nth Fibonacci number using recursion.
💡 Hint
fib(n) = fib(n -
1) + fib(n - 2). Base cases: n=0 or n=1. 4. Power of a Number (Recursive)¶
Rating: Intermediate | Difficulty: Medium
Calculate base^exponent using recursion.
💡 Hint
power(b, e) = b *
power(b, e - 1). Base case: e = 0. 5. Sum of Digits (Recursive)¶
Rating: Intermediate | Difficulty: Medium Extract and sum digits of a number using recursion.
💡 Hint
return (num
% 10) + sumDigits(num / 10). 6. Reverse a String (Recursive)¶
Rating: Intermediate | Difficulty: Medium Print a string in reverse using a recursive function.
💡 Hint
Print the last character first, then call the function for the rest of the string.7. Count Digits¶
Rating: Beginner | Difficulty: Easy Count how many digits are in a number using recursion.
💡 Hint
return 1
+ count(n / 10). Base case: n < 10. 8. GCD using Recursion¶
Rating: Advanced | Difficulty: Medium Implement the Euclidean algorithm recursively to find the Greatest Common Divisor.
💡 Hint
gcd(a, b) = gcd(b, a % b). Base case: b == 0.
9. Palindrome Check (Recursive)¶
Rating: Advanced | Difficulty: Medium Check if a string is a palindrome using recursion.
💡 Hint
Check iffirst == last, then recurse for the inner substring. 10. Binary Search (Recursive)¶
Rating: Advanced | Difficulty: Hard Implement Binary Search on a sorted array using recursion.
💡 Hint
Findmid. If key < mid, search left half; else search right half. 11. Array Traversal¶
Rating: Intermediate | Difficulty: Medium Print all elements of an array recursively.
💡 Hint
Pass the array and the current index to the function.12. Decimal to Binary (Recursive)¶
Rating: Advanced | Difficulty: Medium Convert a decimal integer to its binary equivalent using recursion.
💡 Hint
Printn % 2 after the recursive call convert(n / 2). 13. Towers of Hanoi¶
Rating: Pro | Difficulty: Hard
Solve the Towers of Hanoi puzzle for N disks.
💡 Hint
The classic recursion problem! Moven-1 disks from A to B using C, move disk n to C, then move n-1 disks
from B to C using A. 14. Find Max in Array (Recursive)¶
Rating: Advanced | Difficulty: Medium Find the maximum element in an array using recursion.
💡 Hint
max(arr[n]) = compare(arr[n-1], max(arr, n-1)). 15. All Permutations of a String¶
Rating: Pro | Difficulty: Hard Generate and print all possible permutations of a given string (e.g., "abc").