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

Class Note

The document describes function overloading and recursion programs in C++. It includes three programs - one comparing numbers using overloaded functions of 2, 3, and 4 parameters, one calculating Fibonacci numbers recursively, and one calculating the sum of numbers from 1 to N recursively. The programs demonstrate function overloading by defining multiple functions with the same name but different parameters. They also demonstrate recursion by defining functions that call themselves to repeatedly execute until a base case is reached.

Uploaded by

Warisha Aamir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Class Note

The document describes function overloading and recursion programs in C++. It includes three programs - one comparing numbers using overloaded functions of 2, 3, and 4 parameters, one calculating Fibonacci numbers recursively, and one calculating the sum of numbers from 1 to N recursively. The programs demonstrate function overloading by defining multiple functions with the same name but different parameters. They also demonstrate recursion by defining functions that call themselves to repeatedly execute until a base case is reached.

Uploaded by

Warisha Aamir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

CP LAB ASSIGNMENT 09

WARISHA AAMIR
02-235231-044
BSIT 1(A)
WARISHA AAMIR
02-235231-044 BSIT 1(A)

CSL-113: Computer Programming Lab


Semester BS CS – 01 & BS IT - 01

Lab 09: Function Overloading and Recursion

FUNCTIONS OVERLOADING

Exercise 1

Write a C++ that contains following functions:


int main():
o prompt user to enter numbers
for comparison. o Minimum
numbers user can enter are 2 and
maximum upto 4. o call
comparison() method with two, three
and four parameters.

int comparison():
o this function determine the
smallest and largest number o
print the smallest and largest
number

2
WARISHA AAMIR
02-235231-044 BSIT 1(A)
CODE:
#include <iostream>
using namespace std;
void comparison(int a,int b)
{
if(a>b)
{ cout<<a<<" is the greatest number among first two "<<endl<<b<<" is the
smallest number among first two "<<endl;

}
else
{
cout<<b<<" is the greatest number among first two "<<endl<<a<<" is the smallest
number among first two "<<endl;
}
}

void comparison (int a,int b, int c)


{
if((a>b&&a>c)&&(b>c ))
{
cout<<a<<" is the greatest number among first three " <<endl<<c<<" is
the smallest number among first three "<<endl;
}
else if((a>b&&a>c)&&(c>b ))
{
cout<<a<<" is the greatest number among first three " <<endl<<b<<" is
the smallest number among first three "<<endl;
}
else if((b>a&&b>c)&&(a>c))
{
cout<<b<<" is the greatest number among first three "<<endl<<c<<"
is the smallest number among first three"<<endl;
}
else if((b>a&&b>c)&&(c>a))
{
cout<<b<<" is the greatest number among first three "<<endl<<a<<" is
the smallest number among first three"<<endl;

3
WARISHA AAMIR
02-235231-044 BSIT 1(A)
}

else if( (c<a&&b>c)&&( a>b ))


{
cout<<c<<" is the greatest number among first three "<<endl<<b<<" is the
smallest number among first three"<<endl;
}
else if((c>a&&b<c)&&(b>a))
{
cout<<c<<" is the greatest number among first three "<<endl<<a<<" is the
smallest number among first three"<<endl;
}
else {
cout<< "all are equal"<<endl;
}
}

void comparison(int a,int b,int c,int d)

{
if ( (a>b&&a>c&&a>d) && (d<b&&d<c))
{
cout<<a<<" is the greatest number from list "<<endl<<d<<" is the
smallest number from list "<<endl;
}
else if ((a>b&&a>c&&a>d) && (c<b&&c<d))
{
cout<<a<<" is the greatest number from list "<<endl<<c<<" is the
smallest number from list "<<endl;
}
else if ( (a>b&&a>c&&a>d) && (b<c&&b<d))
{
cout<<a<<" is the greatest number from list "<<endl<<b<<" is the
smallest number from list"<<endl;
}
else if((b>a&&b>c&&b>d) && (d<a&&d<c))
{
cout<<b<<" is the greatest number from list "<<endl<<d<<" is the smallest number
from list"<<endl;

4
WARISHA AAMIR
02-235231-044 BSIT 1(A)
else if((b>a&&b>c&&b>d) && (c<a&&c<d))
{
cout<<b<<" is the greatest number from list "<<endl<<c<<" is the smallest
number from list "<<endl;
}
else if((b>a&&b>c&&b>d) && (a<c&&a<d))
{
cout<<b<<" is the greatest number from list "<<endl<<a<<" is the smallest number
from list "<<endl;
}

else if((c>a&&c>b&&c>d) && (d<a&&d<b))


{
cout<<c<<" is the greatest number from list "<<endl<<d<<" is the smallest
number from list "<<endl;
}
else if((c>a&&c>b&&c>d) && (b<a&&b<d))
{
cout<<c<<" is the greatest number from list "<<endl<<b<<" is the smallest
number from list "<<endl;
}
else if((c>a&&c>b&&c>d) && (a<b&&a<d))
{
cout<<c<<" is the greatest number from list "<<endl<<a<<" is the smallest
number from list "<<endl;
}
else if((d>a&&d>b&&d>c) && ( c<a && c<b))
{cout<<d<<" is the greatest number from list "<<endl<<c<<" is the
smallest number from list"<<endl;}

else if((d>a&&d>b&&d>c) && ( b<a && b<c))


{cout<<d<<" is the greatest number from list "<<endl<<b<<" is the smallest
number from list"<<endl;}

else if((d>a&&d>b&&d>c) && ( a<b && a<c))


{cout<<d<<" is the greatest number from list "<<endl<<a<<" is the smallest
number from list "<<endl;}
else {
cout<<"all are equal"<<endl;

5
WARISHA AAMIR
02-235231-044 BSIT 1(A)

int main()
{
int num1,num2,num3,num4;
cout<<"Enter four numbers"<<endl;
cin>>num1;
cin>>num2;
cin>>num3;
cin>>num4;
comparison(num1,num2);
comparison(num1,num2,num3);
comparison(num1,num2,num3,num4);

return 0;
}

OUTPUT:

6|Page
WARISHA AAMIR
02-235231-044 BSIT 1(A)
Exercise 2

Write a C++ program that perform following task:


int main():
• Ask user to enter a positive number, store it in variable N.
• You have to calculate Fibonacci number with function int fab().
• Print the result. int
fab()
:
• This function calculates the Fibonacci
number. o 0, 1, 1, 2, 3, 5,
8, 13, 21, 34,..
o fab(0) = 0,
fab(1) = 1 o fab(n) = fab(n-
1)
+ fab(n-2) where n>1 
This function must be
recursive function.

CODE:

#include<iostream>

using namespace std;

int fab(int n)
{
if((n==1)||(n==0))
{
return(n);
}
else
{
7|Page
WARISHA AAMIR
02-235231-044 BSIT 1(A)

8|Page
WARISHA AAMIR
02-235231-044 BSIT 1(A)
return(fab(n-1)+fab(n-2));
}
}

int main()
{
int n,i=0;
cout<<"Input the number of terms for Fibonacci
Series:"; cin>>n;
cout<<"--------Fibonacci Series is as follows-----"<<endl;

while(i<n)
{
cout<<" "<<fab(i);
i++;
}

return 0;}

OUTPUT:

9|Page
WARISHA AAMIR
02-235231-044 BSIT 1(A)

Exercise 3

Write a C++ program that performs following task:


int main():
• ask user to enter a positive number, store it in variable N.
• You have to calculate 1+2+3+4+……+N with function int sum().
• Print the result. int sum():

• this function calculate the sum of series from 1 to N.


• this function must be recursion function.

CODE:

#include<iostream>

using namespace std;

int sum (int n)


{
if(n<=0)
{
return(n);
}
else
{
return n+sum(n-1);

}
}

int main()

10 | P a g
e
WARISHA AAMIR
02-235231-044 BSIT 1(A)
{
int n;
cout<<"Enter a number"<<endl;
cin>>n;

for( int i =1 ;i<=n;i++)


{ cout<<i<<"+";
}
int result=sum(n);
cout<<endl<<"Sum is "<<result;
return 0;}

OUTPUT:

11 | P a g
e

You might also like