You are on page 1of 5

Data structures and Algorithms Lab report.

SN. 1

Questions:

1. Find the factorial of a number using recursion.

Algorithm:

Step 1: Start
Step 2: input n
Step 3: print factorial(n)
Step 4: exit

factorial (int x) {}

a. If x == 1 or x == 0 return 1
b. else return n * factorial(n-1)

Program:
Output:

2. Find the sum of natural number using recursion.

Algorithm:

Step 1: Start
Step 2: input n
Step 3: print sumOfN (n)
Step 4: exit

sumOfN (int n) {}

a. If n == 1 return 1
b. else return n + sumOfN (n-1)

Program:
Output:

3. Find the Fibonacci series from a lower range to upper range with recursion.

Algorithm:

Step 1: Start
Step 2: input l, u
Step 3: loop from i = l to <= u and print fib (i)
Step 4: exit

Fib (int x) {}
a. if x == 0 return 0
b. else if x == 1 return 1
c. else return fib (x-1) + fib(x-2)

Program:
Output:

4. Solve the tower of Hanoi with n plates and 3 rods, where A is source, B is temporary and c is
destination.

Algorithm:

Step 1: Start
Step 2: input n
Step 3: call TOH (n, source, temp, destination)
Step 3: exit
void TOH (int n, char source, char temp, char destination) {}

a. If n == 0 return
b. TOH (n-1, source, destination, temp)
c. Move disk n from source to destination
d. TOH (n-1, source, temp, destination)

Program:
Output:

You might also like