Name: Hafsah Ayub
Enrollment No: 02-134191-032
Class : 1-B- BS(CS)
        Lab 10: Function Overloading and Recursion
Function Overloading:
Exercise 1: Write a C++ program for calculating grades of students.
int main():
   prompt user for the number of subjects for which he/she wants to calcuclate the
grade 2 or 3.
   call calgrades() according to their selection.
o For example calgrades(s1,s2) // for two subjects.
o calgrades(s1,s2,s3) // for three subjects.
Source Code:
#include<iostream>
using namespace std;
char calgrades(int x, int y);
char calgrades(int x, int y, int z);
int main()
{
        int x;
        int mark1,mark2,mark3;
        cout<<"How many subjects do you want to calculate the grade of? ";
               cin>>x;
         if(x==2)
               {
               cout<<"Enter marks of subject 1 ";
               cin>>mark1;
               cout<<"Enter marks of subject 2 ";
               cin>>mark2;
               char r= calgrades(mark1,mark2);
               cout<<"Grade "<<r;
               }
         else if(x==3)
         {
               cout<<"Enter marks of subject 1 ";
               cin>>mark1;
               calgrades(mark1,mark2,mark3);
               cout<<"Enter marks of subject 2 ";
               cin>>mark2;
               calgrades(mark1,mark2,mark3);
               cout<<"Enter marks of subject 3 ";
               cin>>mark3;
               char r=calgrades(mark1, mark2, mark3);
Name: Hafsah Ayub
Enrollment No: 02-134191-032
Class : 1-B- BS(CS)
              cout<<"Grade "<<r;
              }
}
char calgrades(int x, int y)
{
        int temp=x+y;
        int sum=temp/2;
        if(sum>=87 && sum<=100)
        return 'A';
        else if (sum>=75 && sum<=86)
        return 'B';
        else if(sum>=65 && sum<=74)
        return 'C';
        else if (sum>=50 && sum<=64)
        return 'D';
        else if (sum<50);
        return 'F';
}
char calgrades(int x, int y, int z)
{
        int temp=x+y+z;
        int sum=temp/3;
        if(sum>=87 && sum<=100)
        return 'A';
        else if (sum>=75 && sum<=86)
        return 'B';
        else if(sum>=65 && sum<=74)
        return 'C';
        else if (sum>=50 && sum<=64)
        return 'D';
        else if (sum<50);
        return 'F';
}
Output:
Name: Hafsah Ayub
Enrollment No: 02-134191-032
Class : 1-B- BS(CS)
Exercise 2: Write a C++ that contains following functions:
int main():
   prompt user to enter numbers for comparison.
   Minimum numbers user can enter are 2 and maximum upto 4.
   call comparison() method with two, three and four parameters.
int comparison():
   this function determine the smallest and largest number
   print the smallest and largest number
Source Code:
#include<iostream>
using namespace std;
void comparison(int a, int b);
void comparison(int a, int b, int c);
void comparison(int a, int b, int c, int d);
int main()
{
       int x,a,b,c,d;
       cout<<"Enter numbers ranging between 2 and 4 ";
       cin>>x;
       if(x==2)
       {
               cout<<"Enter two numbers ";
               cin>>a>>b;
               cout<<"*****************************"<<endl;
               comparison(a,b);
       }
       if(x==3)
       {
               cout<<"Enter three numbers ";
               cin>>a>>b>>c;
               cout<<"*****************************"<<endl;
               comparison(a,b,c);
       }
       if(x==4)
       {
               cout<<"Enter four positive numbers ";
               cin>>a>>b>>c>>d;
               cout<<"***********************************"<<endl;
               comparison(a,b,c,d);
       }
}
void comparison(int a, int b)
{
Name: Hafsah Ayub
Enrollment No: 02-134191-032
Class : 1-B- BS(CS)
       if(a>b)
       {
        cout<<"Smallest among first two is "<<a<<endl;
        cout<<"Largest among first two is "<<b;
        cout<<"******************************"<<endl;
       }
       else
       {
       cout<<"Smallest among first two is "<<b<<endl;
       cout<<"Largest among first two is"<<a<<endl;
       cout<<"******************************"<<endl;
       }
}
void comparison(int a, int b, int c)
{
       if(a<b && a<c)
       {
                cout<<"Smallest among first three is "<<a;
       }
       else if(b<a && b<c)
       {
                cout<<"Smallest among first three is"<<b;
       }
       else if(c<a && c<b)
       {
                cout<<"Smallest among first three is "<<c;
       }
       cout<<endl;
       if(a>b && a>c)
       {
                cout<<"Largest among first three is "<<a;
       }
       else if(b>a && b>c)
       {
                cout<<"Largest among first three is "<<b;
       }
       else if(c>a && c>b)
       {
                cout<<"Largest among first three is "<<c;
       }
       cout<<endl;
       cout<<"************************************";
}
void comparison(int a, int b, int c, int d)
{
       if(a>b && a>c && a>d)
Name: Hafsah Ayub
Enrollment No: 02-134191-032
Class : 1-B- BS(CS)
       {
              cout<<"Largest among in the list is "<<a;
       }
       else if(b>a && b>c && b>d)
       {
                cout<<"Largest number among in the list is "<<b;
       }
       else if(c>a && c>b && c>d)
       {
                cout<<"Largest number among in the list is "<<c;
       }
       else if(d>a && d>b && d>c)
       {
                cout<<"Largest number among in the list is "<<d;
       }
cout<<endl;
  if(a<b && a<c && a<d)
       {
                cout<<"Smallest number among in the list is "<<a;
       }
       else if(b<a && b<c && b<d)
       {
                cout<<"Smallest number among in the list is "<<b;
       }
       else if(c<a && c<b && c<d)
       {
                cout<<"Smallest number among in the list is "<<c;
       }
       else if(d<a && d<b && d<c)
       {
                cout<<"Smallest among in the list is "<<d;
       }
       cout<<endl;
       cout<<"***************************************"<<endl;
}
Output:
Name: Hafsah Ayub
Enrollment No: 02-134191-032
Class : 1-B- BS(CS)
Recursion:
Exercise 3: Write a recursive function that prints the numbers between 1 to n in a
reverse order.
Source Code:
#include<iostream>
using namespace std;
int rev(int x);
int main()
{
       int n;
       cout<<"Enter a number: ";
       cin>>n;
       rev(n);
}
int rev(int x)
{
       if(x<=0)
       return 1;
       else
       cout<<x<<" ";
       return rev(x-1);
}
Name: Hafsah Ayub
Enrollment No: 02-134191-032
Class : 1-B- BS(CS)
Output:
Exercise 4: 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.
Source Code:
#include<iostream>
using namespace std;
int sum(int x);
int main()
{
       int n;
       cout<<"Enter a positive number ";
       cin>>n;
       int sums=sum(n);
       cout<<"The sum of Positive N("<<n<<")"<<" is :"<<sums;
}
int sum(int x)
{
       if(x==1)
       return 1;
       else
       return x+sum(x-1);
}
Name: Hafsah Ayub
Enrollment No: 02-134191-032
Class : 1-B- BS(CS)
Output:
Exercise 5: 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.
Source Code:
#include<iostream>
using namespace std;
int fab(int x);
int main()
{
        int n;
        cout<<"Enter a positive number : ";
        cin>>n;
        cout<<"Fibonacci series is : ";
        for(int i=0; i<n; i++)
        {
                 cout<<" "<<fab(i);
        }
}
int fab(int x)
{
        if(x==0 || x==1)
        return x;
        else if(x>1)
        return (fab(x-1)+fab(x-2));
}
Output: