EXPT1 - Concepts OOPS
EXPT1 - Concepts OOPS
#include<iostream>
using namespace std;
int main()
{
cout<<"area of circle= "<<areaCircle(4)<<endl;
cout<<"area of rectangle= "<<areaRect()<<endl;
return 0;
}
OUTPUT:
area of circle= 50.24
area of rectangle= 1
2) Area of Circle:
#include<iostream>
using namespace std;
int main()
{
const double pi=3.14;
double r=3, area;
area=r*r*pi;
cout<<"area= "<<area<<endl;
return 0;
}
OUTPUT:
area= 28.26
3) Factorial of a number:
#include<iostream>
using namespace std;
int main()
{
int fact=1,i=1,n;
cout<<"Enter a number:"<<endl;
cin>>n;
if(n==0)
cout<<"fact=1"<<endl;
else
{
while(i<=n)
{
fact*=i;
i++;
}
cout<<"fact= "<<fact<<endl;
}
return 0;
}
OUTPUT:
Enter a number:4
fact= 24
4) Maximum of 3 numbers:
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter 3 numbers:\n";
cin>>a>>b>>c;
if(a>b)
{
if(a>c)
{
cout<<"max= "<<a;
}
else
{
cout<<"max= "<<b;
}
}
else
{
if(b>c)
{
cout<<"max= "<<b;
}
else
{
cout<<"max= "<<c;
}
}
return 0;
}
OUTPUT:
Enter 3 numbers:
546
max= 4
5) Mean, variance and std_dev :
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
double arr[6],var=0;
double mean=0,std_dev=0;
cout<<"Enter 6 numbers:"<<endl;
for(int i=0;i<6;i++)
{
cin>>arr[i];
mean+=arr[i];
}
mean/=6.0;
for(int i=0;i<6;i++)
{
var=var+pow((arr[i]-mean),2);
}
var/=6.0;
std_dev=sqrt(var);
cout<<"mean= "<<mean<<endl;
cout<<"\nvariance= "<<var<<endl;
cout<<"\nstd_dev= "<<std_dev<<endl;
return 0;
}
OUTPUT:
Enter 6 numbers:
123456
mean= 3.5
variance= 2.91667
std_dev= 1.70783
6) Sum of 2 numbers:
#include<iostream>
using namespace std;
int main()
{
int a,b,sum;
cout<<"Enter the 2 numbers to be added:";
cin>>a>>b;
sum=a+b;
cout<<"\nSum="<<sum<<endl;
return 0;
}
OUTPUT:
Sum=5
7) Using user defined namespace:
#include<iostream>
namespace calculator
{
int add(int x, int y)
{ return(x+y); }
OUTPUT:
sum= 5
sub= 1
mul= 6
div= 2
8) Using namespace defined in an external header:
calc.h :
namespace calc
{
int add(int x, int y)
{ return(x+y); }
Ext.cpp :
#include<iostream>
#include"calc.h"
using namespace std;
using namespace calc;
int main()
{
cout<<"sum= "<<add(2,3)<<endl;
cout<<"sub= "<<sub(5,4)<<endl;
cout<<"mul= "<<mul(2,3)<<endl;
cout<<"div= "<<divi(4,2)<<endl;
}
OUTPUT:
sum= 5
sub= 1
mul= 6
div= 2
9) Multiples of 3:
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter the numbers:";
cin>>num;
if(num%3==0)
{
cout<<"number is a multiple of 3";
}
else
{
cout<<"number is not a multiple of 3";
}
return 0;
}
OUTPUT: