PF-CE Lab12 Recursive Algorithms
PF-CE Lab12 Recursive Algorithms
(CL1002)
LABORATORY MANUAL
BS-CE, Spring 2025
LAB 12
Implementation of Recursive Algorithms
______________________________________
Lab Objectives:
1. To learn about Recursion .
Software Required:
Code: Dev C++
Introduction:
1. Recursive Functions
A recursive function is one that calls itself. You have seen instances of
functions calling other functions. Function A can call function B, which can
then call function C. It’s also possible for a function to call itself. A function
that calls itself is a recursive function. Look at this message function:
void message()
{
cout << "This is a recursive function.\n";
message();
}
This function displays the string “This is a recursive function.\n” and then
calls itself. Each time it calls itself, the cycle is repeated. There’s no way to
stop the recursive calls. This function is like an infinite loop because there is
no code to stop it from repeating. The function message will eventually
cause the program to crash. Eventually, these recursive function calls will
use up all available stack memory and cause it to overflow.
Like a loop, a recursive function must have some method to control the
number of times it repeats. The following is a modification of the message
function. It passes an integer argument, that holds the number of times the
function is to call itself.
message(5);
The argument, 5, will cause the function to call itself five times. The first
time the function is called, the if statement will display the message and
then call itself with 4 as the argument.
As you can see from Figure that the function is called a total of six times.
The first time it is called from main, and the other five times it calls itself, so
the depth of recursion is five. When the function reaches its sixth call, the
times parameter will be set to 0. At that point, the if statement’s conditional
expression will be false. So, the function will return. Control of the program
will return from the sixth instance of the function to the point in the fifth
instance directly after the recursive function call:
Example 1:
#include <iostream>
using namespace std;
void message(int);
int main()
{
message(5);
return 0;
}
To further illustrate the inner workings of this recursive function, let’s look at
another version of the program.
Example 2:
#include <iostream>
using namespace std;
void message(int);
int main()
{
message(5);
return 0;
}
Example 3:
#include <iostream>
using namespace std;
int factorial(int);
int main()
{
int number;
int factorial(int n)
{
if (n == 0)
return 1; // Base case
else
return n * factorial(n − 1); // Recursive case
}
Tasks
Task01:
int main()
{
int number;
cout << "Enter an integer value and I will display the \n";
cout << "factorials of all the numbers smaller than it including the number: ";
cin >> number;
for(int i =number;i>=number,i>=0;i--){
cout << "The factorial of " << i << " is ";
cout << factorial(i) << endl;}
return 0;
}
int factorial(int n)
{
if (n == 0)
return 1; // Base case
else
return n * factorial(n - 1); // Recursive case
}
Task02:
#include <iostream>
#include <cmath>
using namespace std;
int Fibo(int);
int main()
{
int n;
cout<<"Enter the nth term of fibonacci series : "<<endl;
cin>>n;
for(int i=0;i<=n;i++)
{cout<<Fibo(i);
if (i != n) {
cout << ", ";
}
}
return 0;
}
int Fibo(int n)
{ if (n == 0)
return 0; // Base case
if (n == 1)
return 1;
else
return Fibo(n-1) + Fibo(n - 2); // Recursive case
}
Task03:
Write a recursive function to calculate the nth Fibonacci number using the following
recursive function.
F(n) = F(n-1) + F(n-2) where F(0) = 0 and F(1) = 1.
#include <iostream>
#include <cmath>
using namespace std;
int Fibo(int);
int main()
{
int n;
cout<<"Enter the nth term of fibonacci series : "<<endl;
cin>>n;
cout<<Fibo(n);
return 0;
}
int Fibo(int n)
Task04:
Write a recursive function to calculate the sum of first n numbers where n is the input.
#include <iostream>
#include <cmath>
using namespace std;
int Sum(int);
int main()
{
int n;
cout<<"Enter the nth number to find the sum of all the number smaller than it : "<<endl;
cin>>n;
cout<<"The sum is : "<<Sum(n) ;
return 0;
}
int Sum(int n)
{ if (n == 0)
return 0; // Base case
else
return n +Sum(n-1); // Recursive case
}
Task05:
int main() {
string myString;
return 0;
}
Bonus Task.
Write a report on the problem of Tower of Hanoi and write the recursive function to solve
the problem. Online help may be taken.
#include<iostream>
using namespace std;
int moveCount = 0;
int main()
{
int number;
cout << "Enter the number of disks: ";
cin >> number;
if (number <= 0)
{
cout << "Please enter a positive number of disks." << endl;
return 1;
}