0% found this document useful (0 votes)
2 views

Practical No. 7 Recursion_ Implement Recursive Algorithms (e.g., Factorial, Fibonacci, Tower of Hanoi).

The document explains the concept of recursion in programming, detailing the steps for implementing recursive algorithms, which include identifying the base case, defining the recursive case, and combining results. It provides examples of recursive algorithms such as factorial, Fibonacci series, and Tower of Hanoi, along with their complexities and practical applications. The document emphasizes the importance of recursion in solving problems that can be divided into smaller subproblems.

Uploaded by

Soham Ghadge
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Practical No. 7 Recursion_ Implement Recursive Algorithms (e.g., Factorial, Fibonacci, Tower of Hanoi).

The document explains the concept of recursion in programming, detailing the steps for implementing recursive algorithms, which include identifying the base case, defining the recursive case, and combining results. It provides examples of recursive algorithms such as factorial, Fibonacci series, and Tower of Hanoi, along with their complexities and practical applications. The document emphasizes the importance of recursion in solving problems that can be divided into smaller subproblems.

Uploaded by

Soham Ghadge
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Practical No.

7
Recursion: Implement recursive algorithms (e.g., factorial, Fibonacci, Tower of
Hanoi).

Recursion is a programming concept where a function calls itself to solve


smaller instances of the same problem. Recursive algorithms are widely used to
solve problems that can be broken into smaller, similar subproblems.
Steps for Implementing Recursive Algorithms:

1. Identify the Base Case:


○ The condition under which the recursion stops (e.g., n=0n = 0n=0 or
n=1n = 1n=1).
2. Define the Recursive Case:
○ The step where the function calls itself with a smaller input,
converging towards the base case.
3. Combine Results:
○ Use the results of the smaller subproblems to compute the final
solution.

Briefing: Recursion

Recursion is a programming concept where a function calls itself to solve


smaller instances of the same problem. Recursive algorithms are widely used to
solve problems that can be broken into smaller, similar subproblems.
Steps for Implementing Recursive Algorithms:

1. Identify the Base Case:


○ The condition under which the recursion stops (e.g., n=0n = 0n=0 or
n=1n = 1n=1).
2. Define the Recursive Case:
○ The step where the function calls itself with a smaller input,
converging towards the base case.
3. Combine Results:
○ Use the results of the smaller subproblems to compute the final
solution.

Implementation of Recursive Algorithms


1. Factorial of a Number
2. Fibonacci Series
3. Tower of Hanoi
Example Outputs:
Factorial:

● Input: 555
● Output: 120120120
● Explanation: 5×4×3×2×1=1205 \times 4 \times 3 \times 2 \times 1 =
1205×4×3×2×1=120
Fibonacci:

● Input: 777
● Output: 131313
● Explanation: Sequence is 0,1,1,2,3,5,8,130, 1, 1, 2, 3, 5, 8,
130,1,1,2,3,5,8,13.
Tower of Hanoi:

● Input: 333 disks

Complexity:

1. Factorial: O(n)O(n)O(n)
2. Fibonacci: O(2n)O(2^n)O(2n) (can be optimized using memoization)
3. Tower of Hanoi: O(2n−1)O(2^n - 1)O(2n−1)

Practical Applications:
● Factorial: Combinatorics, probability calculations.
● Fibonacci: Financial modeling, search algorithms.
● Tower of Hanoi: Problem-solving and algorithm analysis.

You might also like