Lab 10 Overloading and Recursion
Lab 10 Overloading and Recursion
Lab 10 Overloading and Recursion
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)
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:
}
Name: Hafsah Ayub
Enrollment No: 02-134191-032
Class : 1-B- BS(CS)
Output: