You are on page 1of 12

Q#1: 

Ask the numbers form the user and swap those numbers without using third variable.
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;
cout<<"before swap a="<<a<<", b="<<b<<endl;
a=a*b;
b=a/b;
a=a/b;
cout<<"after swaping a="<<a<<", b="<<b<<endl;
return 0;
}

***
Q#2: Write a function that accepts a number from the user, calculates its
factorial and returns it. The program should ask the user if he wants to continue
for more numbers. If the user presses the key “Y” the program should keep
asking for a number and printing its factorial till the user hits the key “N”.

#include<iostream>
using namespace std;
long factorial(int num)
{
if(num==1||num==0)
return 1;
else
return num*factorial(num-1);
}
int main()
{
int a;
char key;
do
{
cout<<"Enter any positive number:";
cin>>a;
cout<< "\nFactorial of"<<a<<"="<< factorial(a)<<endl;
cout<<"Next number Factorial? Y/N:";
cin>>key;
while((key != 'y') && (key != 'Y') && (key != 'N') && (key != 'n'))
{
cout<<"please enter key Y/N: ";
cin>>key;
}
}
while(key=='y'||key=='Y');
return 0;
}
***
Q#3: . Write a program of function overloading that has a function Convert()
that accepts a value in parameter and convert it into string and print on screen.
Write for
· Convert(int num) e.g. Convert(137) will output One Hundred and Thirty Seven
· Convert(double num) e.g. Convert(137.9) will output One Hundred and Thirty
Seven Point Nine

#include<iostream>
#include<string.h>
using namespace std;
string Convert(int n)
{ string word;
string unit_teen[]={"","one","two","three","four","five","six","seven","eight","nine","ten",

"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}
;
string ten[]={"","","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
string hundred[]={"","one hundred","two hundred","three hundred","four hundred","five
hundred","six hundred",
"seven hundred","eight hundred","nine hundred"};
if(n<0 && n>=1000)
cout<<"\nError:";
else if(n>=0 && n<=19)
{ word = unit_teen[n];
return word;
}
else if(n>=20 && n<=99)
{ word = ten[n/10] + " " + unit_teen[n%10];
return word;
}
else
{ word = hundred[n/100] + " " +Convert(n%100);
return word;
}
}
string Convert(double n)
{ int n1=n;
int n2=n*100;
string word;
string
unit_teen[]={"zero","one","two","three","four","five","six","seven","eight","nine","ten",

"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}
;
string ten[]={"","","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
string hundred[]={"","one hundred","two hundred","three hundred","four hundred","five
hundred","six hundred",
"seven hundred","eight hundred","nine hundred"};
if(n<0 && n>=1000)
cout<<"\nError:";
else if(n>=0 && n<=19)
{ word = unit_teen[n1] + " point " + unit_teen[(n2/10)%10];
return word;
}
else if(n>19 && n<=99)
{ word = ten[n1/10] + unit_teen[n1%10] + " point " + unit_teen[(n2/10)%10] +
unit_teen[n2%10];
return word;
}
else
{ word = hundred[n1/100] ;
word+= " and ";
word+= Convert(n1%100) ;
return word + " point " + unit_teen[(n2/10)%10] + unit_teen[n2%10];
}
}
int main()
{ int test1;
double test2;
string s;
cout<<"\nEnter whole number in range 1-999:";
cin>>test1;
s = Convert(test1);
cout<<"\n"<<s;
cout<<"\nEnter decimal number in range 1-999:";
cin>>test2;
s=Convert(test2);
cout<<"\n"<<s;
}
***

Q#4: Write a program where you have three types of Employees.


1. Manager
2. Administrator
3. Clerk
Focusing on Polymorphic behavior write code for these functions.
· string GetName()
· void CalculateSalary()
   o For Manager Salary = Basic Salary + Medical Allowance + Utility Bills + TADA
   o For Administrator = Basic Salary + Medical Allowance + Utility Bills
   o For Clerk = Basic Salary + Medical Allowance
· void PrintSalary()

#include<iostream>
#include<string>
using namespace std;
class employee
{
protected:
string name;
float salary,basic,medical,utility,TADA;
public:
string getName()
{
return name;
}
virtual void calculateSalary() = 0;
virtual void printSalary() = 0;
};
class manager : public employee
{
public:
manager(string mName)
{
name = mName;
basic = 50000;
medical = 10000;
utility = 3000;
TADA = 2000;
}
void calculateSalary()
{
salary = basic + medical + utility + TADA;
}
void printSalary()
{
cout<<"Salary of manager " <<name<<" = "<<salary<<endl;
}
};
class administrator : public employee
{
public:
administrator(string aName)
{
name = aName;
basic = 40000;
medical = 10000;
utility = 2000;
}
void calculateSalary()
{
salary = basic + medical + utility;
}
void printSalary()
{
cout<<"Salary of administrator " <<name<<" = "<<salary<<endl;
}
};
class clerk : public employee
{
public:
clerk(string cName)
{
name = cName;
basic = 30000;
medical = 5000;
}
void calculateSalary()
{
salary = basic + medical;
}
void printSalary()
{
cout<<"Salary of clerk " <<name<<" = "<<salary<<endl;
}
};
int main()
{
employee *emp1 = new manager("Usman Hayyat");
cout<<emp1->getName();
emp1->calculateSalary();
emp1->printSalary();
cout<<endl;
employee *emp2 = new administrator("Asad Karim");
cout<<emp2->getName();
emp2->calculateSalary();
emp2->printSalary();
cout<<endl;
employee *emp3 = new clerk("Faisal Amanat");
cout<<emp3->getName();
emp3->calculateSalary();
emp3->printSalary();
}
***

Q#5: . Write a program that makes use of Polymorphism and draws different
Shapes. The function to draw a shape would be Draw(), the program should
allow to draw Square, Triangle and Circle. The program should ask the user
what is to be drawn. If user selects to draw Square, the function Draw() should
output “A Square has been drawn.”
#include<iostream>
using namespace std;
class shape
{
public:
virtual void Draw() = 0;
};
class triangle : public shape
{
public:
void Draw()
{
cout<< "Triangle has been drawn."<<endl;
}
};
class circle : public shape
{
public:
void Draw()
{
cout<< "Circle has been drawn."<<endl;
}
};
class rectangle : public shape
{
public:
void Draw()
{
cout<< "Rectangle has been drawn."<<endl;
}
};
int main()
{
int choice;
cout<<" which shape is to be drawn? 1,2 or 3"<<endl;
cout<<"1:Rectangle, 2: Triangle, 3: Circle"<<endl;
cin>>choice;
if(choice == 1)
{
shape *testShape = new rectangle();
testShape->Draw();
}
else if(choice == 2)
{
shape *testShape = new triangle();
testShape->Draw();
}
else if(choice == 3)
{
shape *testShape = new circle();
testShape->Draw();
}
else
{
cout<<"invalid choice";
}
return 0;
}

You might also like