PROGRAMMING IN C++
3330702
[PRACTICAL-NO]
•Explain basic concepts of object-oriented programming.
Enrollment Number: 206250307018
Name of the student: AAFIKHAN MALEK.
Your practical exercise page.
206250307018
1. Write a C++ program to find the older brother out of given the age
of two brothersusing functions.(Call by value).
#include<iostream>
using namespace std;
int findOlder(int,int);
int main()
int p,d;
cout<<"Enter age of abc: "<<endl;
cin>>p;
cout<<"Enter age of xyz: "<<endl;
cin>>d;
if(p==findOlder(p,d))
cout<<"abc is older brother";
else
cout<<"xyz is older brother";
return 0;
int findOlder(int age1,int age2)
{
if(age1 > age2)
return age1;
else
return age2;
Output -
2. Write a C++ program to swap the value of two integer variables
using functions. (Use Callby reference, donot use third variablefor
swapping).
#include<iostream>
using namespace std;
void swap(int &,int &);
int main()
int a=11,b=22;
cout<<"Befor swapping"<<endl;
cout<<"A= "<<a<<endl;
cout<<"B= "<<b<<endl;
swap(a,b);
cout<<"After swapping"<<endl;
cout<<"A= "<<a<<endl;
cout<<"B= "<<b<<endl;
return 0;
void swap(int &x,int &y)
x=x+y;
y=x-y;
x=x-y;
Output -
3. Write a C++ program to find the older brother out of given the age
of two brothers using functions. (Use Return by reference).
#include<iostream>
using namespace std;
int OlderBrother(int,int);
int main()
int x,y;
cout<<"Enter age of X: "<<endl;
cin>>x;
cout<<"Enter age of Y: "<<endl;
cin>>y;
if(x==OlderBrother(x,y))
cout<<"X is older brother"<<endl;
else
cout<<"Y is older brother"<<endl;
return 0;
int OlderBrother(int x,int y)
if(x > y)
return x;
else
{
return y;
Output -
4. Write a C++ program to find the cube of the given integer
numberusing inline functions.
#include<iostream>
using namespace std;
inline double cube(double n)
return n*n*n;
int main()
cout<<"cube :"<<cube(5);
return 0;
}
Output -
5. Write a C++ program to calculate the simple interest using
functions. (Set rate of interest0.9 as the default arguments).
#include<iostream>
using namespace std;
float SI(float p,float n,float r=0.09)
return p*n*r;
int main()
cout<<"the SIMPLE INTREST IS :"<<SI(2000,2)<<endl;
return 0;
Output -
6. Write a C++ programto calculate the area of square,
rectangleandcircle usingfunctionoverloading.
#include<iostream>
#define pi 3.14
using namespace std;
double area(int a)
return a*a;
double area(double b,double l)
return b*l;
double area(float r)
return pi*r*r;
int main()
int a,b,l;
float r;
cout<<"Enter square of a :";
cin>>a;
cout<<"Area of square is :"<< area(a)<<endl<<endl;
cout<<"Enter breath and length :";
cin>>b>>l;
cout<<"Area of rectangle is :"<<area(b,l)<<endl<<endl;
cout<<"Enter radius of circle :";
cin>>r;
cout<<"Area of circle is :"<<area(r)<<endl<<endl;
return 0;
Output -
7. Write a C++ program to calculate the volume of cube, cylinder and
rectangular box using function overloading.
#include<iostream>
#define pi 3.14
using namespace std;
int volume(int a)
return a*a*a;
double volume(double r,int h)
{
return pi*r*r*h;
long volume(long l,int b,int h)
return l*b*h;
int main()
cout<<"volume of a cube :"<<volume(3)<<endl;
cout<<"volume of a cylinder :"<<volume(2.0,10)<<endl;
cout<<"volume of a rectangle :"<<volume(5,6,7)<<endl;
return 0;
Output -
8. Write a C++ program to demonstrate the use of any twoin-built
math library functions.
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double a,b,c;
a=121;
b=7;
c=3;
cout<<"Square root of "<<a<<" is "<<sqrt(a)<<endl;
cout<<b<<" " <<"raised to "<<c<<" is "<<pow(b,c);
return 0;
Output -