0% found this document useful (0 votes)
20 views

EXPT1 - Concepts OOPS

Uploaded by

konuriyash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

EXPT1 - Concepts OOPS

Uploaded by

konuriyash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PROGRAMS:

1) Area of circle and rectangle:

#include<iostream>
using namespace std;

double areaCircle(double r,double pi=3.14)


{
return(r*r*pi);
}

double areaRect(double l=1,double b=1)


{
return(l*b);
}

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:

Enter the 2 numbers to be added:2 3

Sum=5
7) Using user defined namespace:
#include<iostream>
namespace calculator
{
int add(int x, int y)
{ return(x+y); }

int sub(int x, int y)


{ return(x-y); }

int mul(int x, int y)


{ return(x*y); }

int divi(int x, int y)


{ return(x/y); }
}

using namespace std;


using namespace calculator;
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;
return 0;
}

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); }

int sub(int x, int y)


{ return(x-y); }

int mul(int x, int y)


{ return(x*y); }

int divi(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:

Enter the numbers:6


number is a multiple of 3

You might also like