Fundamentals of Recursion: Unlocking the Power of Self-Reference with solving basic recursion problems
What is Recursion?
Recursion is a programming technique where a function calls itself, either directly or indirectly, to solve a problem. In simple terms, when a function calls itself to solve a problem, it's called recursion, and the function itself is called a recursive function. In recursion, a problem gets solved by breaking it into smaller instances of the same problem until reaching a base case. This base case acts as the termination condition, preventing infinite recursion. It is a powerful concept that enables elegant solutions to specific problems by breaking them down into smaller, more manageable instances.
Direct vs Indirect Recursion :
Direct Recursion: In direct recursion, a function directly calls itself within its own body. This is the most straightforward form of recursion. When the function encounters a recursive call, it executes the function's body again, potentially with different arguments. For an example :
def Direct_Recursion():
#Execute base case and other codes
Direct_Recursion()
Indirect Recursion: In indirect recursion, there is a chain of function calls where one function calls a second function, which in turn calls the first function or another function that eventually calls the first function. It involves two or more functions calling each other in a circular manner. Here is an example of indirect recursion:
def indirect_Recusion_1():
#Execute base case and other codes
indirect_Recusion_2()
def indirect_Recusion_2():
#Execute base case and other codes
indirect_Recusion_1()
Principles of Recursion:
Base Case: A condition where the recursion stops. Without a base case, the function would infinitely call itself, leading to a stack overflow error.
Recursive Step: A step where the function calls itself with modified parameters, progressing towards the base case.
Divide and Conquer: Recursion follows the "divide and conquer" strategy, breaking down a problem into smaller, more manageable subproblems.
Lets try to understand by using a Factorial Calculation code :
def factorial(n):
# Base case: factorial of 0 or 1 is 1
if n == 1:
return 1
# Recursive step: n! = n * (n-1)!
else:
return n * factorial(n - 1)
# Test the function
print(factorial(5)) # Output: 120
Now, let's try to visualize this code :
Here:
Step-by-step Visualization:
Initial Call:
factorial(5)
n
is 5. Sincen
is not 1, we proceed to the recursive step.
First Recursive Call:
factorial(4)
n
is 4. Sincen
is not 1, we proceed to the next recursive call.
Second Recursive Call:
factorial(3)
n
is 3. Sincen
is not 1, we proceed to the next recursive call.
Third Recursive Call:
factorial(2)
n
is 2. Sincen
is not 1, we proceed to the next recursive call.
Fourth Recursive Call:
factorial(1)
n
is 1, which matches the base case. We return 1.
Backtracking:
We return to the previous call (
factorial(2)
) with the result offactorial(1)
, which is 1.factorial(2)
returns2 * 1
, which is 2.
Backtracking:
We return to the previous call (
factorial(3)
) with the result offactorial(2)
, which is 2.factorial(3)
returns3 * 2
, which is 6.
Backtracking:
We return to the previous call (
factorial(4)
) with the result offactorial(3)
, which is 6.factorial(4)
returns4 * 6
, which is 24.
Backtracking:
We return to the initial call (
factorial(5)
) with the result offactorial(4)
, which is 24.factorial(5)
returns5 * 24
, which is 120.
So, the chain of recursive calls unwinds, and each factorial is calculated accordingly:
factorial(1)
returns 1.
factorial(2)
returns 2 * 1 = 2
.
factorial(3)
returns 3 * 2 = 6
.
factorial(4)
returns 4 * 6 = 24
.
factorial(5)
returns 5 * 24 = 120
.
Now it's your turn to solve some basic recursive-based problems.
All the solutions are given at the end of all problems. First, try to solve these problems by yourself. Best of luck! Happy coding!!
1. Write a program in C/C++/Python to print the first 50 natural numbers using recursion.
Expected Output:
The natural numbers are :
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
2. Write a program in C/C++/Python to calculate the sum of numbers from 1 to n using recursion.
Test Data :
Input the last number of the range starting from 1 : 5
Expected Output:
The sum of numbers from 1 to 5 :
15
3. Write a program in C/C++/Python to print the Fibonacci Series using recursion.
Test Data :
Input number of terms for the Series (< 20) : 10
Expected Output:
Input number of terms for the Series (< 20) : 10
The Series are :
1 1 2 3 5 8 13 21 34 55
4. Write a program in C/C++/Python to print the array elements using recursion.
Test Data :
Input the number of elements to be stored in the array :6
Input 6 elements in the array :
element - 0 : 2
element - 1 : 4
element - 2 : 6
element - 3 : 8
element - 4 : 10
element - 5 : 12
Expected Output :
The elements in the array are : 2 4 6 8 10 12
5. Write a program in C/C++/Python to count the digits of a given number using recursion.
Test Data :
Input a number : 50
Expected Output :
The number of digits in the number is : 2
6. Write a program in C/C++/Python to find the sum of digits of a number using recursion.
Test Data :
Input any number to find sum of digits: 25
Expected Output:
The Sum of digits of 25 = 7
7. Write a program in C/C++/Python to find the GCD of two numbers using recursion.
Test Data :
Input 1st number: 10
Input 2nd number: 50
Expected Output :
The GCD of 10 and 50 is: 10
8. Write a program in C/C++/Python to get the largest element of an array using recursion.
Test Data :
Input the number of elements to be stored in the array :5
Input 5 elements in the array :
element - 0 : 5
element - 1 : 10
element - 2 : 15
element - 3 : 20
element - 4 : 32
Expected Output :
Largest element of an array is: 32
9. Write a program in C/C++/Python to reverse a string using recursion.
Test Data :
Input any string: Imran32
Expected Output:
The reversed string is: 23narmI
10. Write a program in C/C++/Python to find the Factorial of a number using recursion.
Test Data :
Input a number : 5
Expected Output:
The Factorial of 5 is : 120
11. Write a program in C/C++/Python to convert a decimal number to binary using recursion.
Test Data :
Input any decimal number : 66
Expected Output :
The Binary value of decimal no. 66 is : 1000010
12. Write a program in C/C++/Python to check if a number is a prime number or not using recursion.
Test Data :
Input any positive number : 7
Expected Output :
The number 7 is a prime number.
13. Write a program in C/C++/Python to find the LCM of two numbers using recursion.
Test Data :
Input 1st number for LCM : 4
Input 2nd number for LCM : 6
Expected Output :
The LCM of 4 and 6 : 12
14. Write a program in C/C++/Python to print even or odd numbers in a given range using recursion.
Test Data :
Input the range to print starting from 1 : 10
Expected Output :
All even numbers from 1 to 10 are : 2 4 6 8 10
All odd numbers from 1 to 10 are : 1 3 5 7 9
15. Write a C/C++/Python program to multiply two matrices using recursion.
Test Data :
Input number of rows for the first matrix : 2
Input number of columns for the first matrix : 1
Input number of rows for the second matrix : 1
Input number of columns for the second matrix : 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [1],[0] : 2
Input elements in the second matrix :
element - [0],[0] : 3
element - [0],[1] : 4
Expected Output :
Here is the elements of First matrix :
1
2
Here is the elements of Second matrix :
3 4
The multiplication of two matrix is :
3 4
6 8
16. Write a C/C++/Python program to check whether a given string is a palindrome or not using recursion.
Test Data :
Input a word to check for palindrome : Imran3232narmI
Expected Output :
The entered word is a palindrome.
17. Write a program in C/C++/Python to calculate the power of any number using recursion.
Test Data :
Input the base value : 2
Input the value of power : 6
Expected Output :
The value of 2 to the power of 6 is : 64
18. Write a C/C++/Python program to find the Hailstone Sequence of a given number up to 1.
Test Data :
Input any number (positive) to start for Hailstone Sequence : 13
Expected Output :
The hailstone sequence starting at 13 is :
13 40 20 10 5 16 8 4 2 1
The length of the sequence is 10.
19. Write a program in C/C++/Python to copy one string to another using recursion.
Test Data :
Input the string to copy : Imran32
Expected Output :
The string successfully copied.
The first string is : Imran32
The copied string is : Imran32
20. Write a program in C/C++/Python to find the first capital letter in a string using recursion.
Test Data :
Input a string to including one or more capital letters : capitalLetter
Expected Output :
The first capital letter appears in the string capitalLetter is L.
21. Write a program in C/C++/Python for binary search using recursion.
Test Data :
Input the number of elements to store in the array :3
Input 3 numbers of elements in the array in ascending order :
element - 0 : 15
element - 1 : 25
element - 2 : 35
Input the number to search : 35
Expected Output :
The search number found in the array.
For the solution, click on the solution link! Solutions