You are on page 1of 5

Practicals

Question Three are three types of employees in Indian railways. They are regular, daily wages
and
consolidated employees. Gross Pay for the employees are calculated as follows:
i. regular employees - basic + hra + % of DA * basic
ii. Daily wages – wages per hour * number of hours
iii. Consolidated – fixed amount

Code #include <bits/stdc++.h>


using namespace std;
float gross(float i){
return i+0.07*i+0.7*i;
}
float gross(int i,int j){
return i*j;
}
int gross(){
return 5000;
}
int main()
{
int time,wph,choice;
float basic;
cin>>choice;
if(choice==1){
cin>>basic;
cout<<gross(basic);
}
else if(choice==2){
cin>>time>>wph;
cout<<gross(time,wph);
}
else if(choice==3){
cout<<gross();
}
else
cout<<"Invalid Choice";
}
Output

Question Define four function void swap () which accepts two arguments by reference and
swap the values. First function swaps two characters, second function swaps two
integers, third function swaps two floats values and fourth function swaps two
double values. Use the concept function overloading and inline function.
Code #include <bits/stdc++.h>
using namespace std;
inline void swap(int &i,int &j)
{
int t=i;
i=j;
j=t;
}
void swap(char &i,char &j)
{
int t=i;
i=j;
j=t;
}
void swap(float &i,float &j)
{
float t=i;
i=j;
j=t;
}
void swap(double &i,double &j)
{
double t=i;
i=j;
j=t;
}
int main()
{
int p,q;
char r,t;
float e,f;
double g,h;
cin>>p>>q;
swap(p,q);
cout<<p<<endl<<q<<endl;
cin>>r>>t;
swap(r,t);
cout<<r<<endl<<t<<endl;
cin>>e>>f;
swap(e,f);
cout<<fixed<<setprecision(2)<<e<<endl<<fixed<<setprecision(2)<<f<<endl;
cin>>g>>h;
swap(g,h);
cout<<g<<endl<<h<<endl;
}

Output

Question Write a program to find out the greatest number and string out of two entered input
values using function overloading concept.
Code #include <bits/stdc++.h>
using namespace std;
int great(int p,int q)
{
return (p>q)?p : q;
}
string great(string p,string q)
{
if(max(p.size(),q.size())==p.size())
return p;
else
return q;
}
int main()
{
int i,j;
string a,b;
cin>>i>>j;
cin>>a>>b;
cout<<great(i,j)<<endl;
cout<<great(a,b)<<endl;
}
Output

Question Write a program to calculate the area of circle, rectangle and triangle using function
overloading.
Code #include <bits/stdc++.h>
using namespace std;
float area(float i)
{
return 3.14*i*i;
}
float area(int i,int j)
{
return i*j;
}
float area(float i,float j)
{
return 0.5*i*j;
}
int main()
{
int a,b,choice;
float p,q,r;
cin>>choice;
if(choice==1){
cin>>r;
cout<<"Area of Circle : "<<fixed<<setprecision(2)<<area(r);
}
else if(choice==2){
cin>>a>>b;
cout<<"Area of Rectangle : "<<fixed<<setprecision(2)<<area(a,b);
}
else if(choice==3){
cin>>p>>q;
cout<<"Area of Triangle : "<<fixed<<setprecision(2)<<area(p,q);
}
else
{
cout<<"Invalid Choice";
}
}
Flowchart

Question Write a program to calculate the volume of Cone, Cylinder and Sphere using function
overloading.
Code #include <bits/stdc++.h>
using namespace std;
double volume(double i,double j)
{
return 3.14*i*i*j;
}
float volume(float i)
{
return 1.33*3.14*i*i*i;
}
float volume(float i,float j)
{
return 0.33*3.14*i*i*j;
}
int main()
{
int choice;
double e,f;
float r,h;
cin>>choice;
if(choice==1)
{
cin>>r>>h;
cout<<fixed<<setprecision(4)<<volume(r,h);
}
else if(choice==2){
cin>>e>>f;
cout<<fixed<<setprecision(4)<<volume(e,f);
}
else if(choice==3){
cin>>r;
cout<<fixed<<setprecision(4)<<volume(r);
}
else
cout<<"Invalid Choice";
}
Output

Question Write a program to compute absolute value for entered integer and float numbers.
Code #include <bits/stdc++.h>
using namespace std;
int absolute(int p){
return abs(p);
}
float absolute(float p){
return abs(p);
}
int main()
{
int i;
float e;
cin>>i;
cin>>e;
cout<<absolute(i)<<endl;
cout<<fixed<<setprecision(2)<<absolute(e)<<endl;
}
Output

You might also like