Visual Studio
Recursion is a programming technique where a function calls itself repeatedly until a specific base condition is met. A function that performs such self-calling behavior is known as a recursive function, and each instance of the function calling itself is called a recursive call.
Recursive Function: A function that calls itself is called a recursive function. When a recursive function is called, it executes a set of instructions and then calls itself to execute the same set of instructions with a smaller input. A recursive function should contain, Recursive Case: Recursive case is the way in which the recursive call is present in the function. Base Condition: The base condition is the condition that is used to terminate the recursion.
Applications of Recursion:
Recursion has many applications in computer science and programming. Here are some of the most common applications of recursion:
Solving: Fibonacci sequences, Factorial Function, Reversing an array, Tower of Hanoi. Backtracking: It is a technique for solving problems by trying out different solutions and undoing them if they do not work. Recursive algorithms are often used in backtracking. Searching and Sorting Algorithms: Many searching and sorting algorithms, such as binary search and quicksort, use recursion to divide the problem into smaller sub-problems. Tree and Graph Traversal: Recursive algorithms are often used to traverse trees and graphs, such as depth-first search and breadth-first search. Mathematical Computations: Recursion is also used in many mathematical computations, such as the factorial function and the Fibonacci sequence. Dynamic Programming: It is a technique for solving optimization problems by breaking them down into smaller sub-problems. Recursive algorithms are often used in dynamic programming.
The concept of recursion was understood by the following codes,
- Factorial using recursion
- sum of integers using recursion
- reversal of string and integers using recursion
Algorithm: Reverse a String Using Recursion
- Start
- Declare a character array
ch[50]
. - Prompt the user to "Enter a string" and read input into
ch
. - Define a recursive function
rev(char* str)
:- If
*str
is not null (\0
): Callrev(str + 1)
recursively. Print the current character*str
.
- If
- In
main()
, callrev(ch)
to display the reversed string. - End
Algorithm: Reverse an Integer Using Recursion
- Start
- Declare an integer variable
a
. - Prompt the user to "Enter the number" and read input into
a
. - Define a recursive function
revint(int a)
:- If
a > 0
:- Print
a % 10
(the last digit ofa
). - Call
revint(a / 10)
recursively (removing the last digit).
- Print
- If
- In
main()
, callrevint(a)
to display the number in reverse. - End
Algorithm: Sum of First n Natural Numbers Using Recursion
- Start
- Declare an integer variable
n
and another variablea
for storing the result. - Prompt the user to "Enter a number" and read input into
n
. - Define a recursive function
add(int n)
:- If
n <= 1
, return 1. - Else, return
n + add(n-1)
(sum ofn
and the sum of firstn-1
numbers).
- If
- In
main()
, calladd(n)
and store the result ina
. - Display the value of
a
. - End
Algorithm: Factorial Using Recursion
- Start
- Declare an integer variable
x
and another variableA
to store the factorial. - Prompt the user to "Enter the number" and read input into
x
. - Define a recursive function
fact(int x)
:- If
x <= 1
, return 1. - Else, return
x * fact(x-1)
(multiplyx
by factorial ofx-1
).
- If
- In
main()
, callfact(x)
and store the result inA
. - Display
"The factorial is : "
followed byA
. - End
The above codes demonstrate the concept of recursion in C++