VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
Oops lab manual
1. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II NEW RAIPUR
Department of ComputerScience and engineering
LAB MANUAL
For
Object Oriented Programming Language (C++)
2. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
Experiment No. 1
Aim: Write a program to check whether number is prime or not.
Source:
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
int num,count=0;
cout<<”Enter the number number to check:”;
cin>>num;
for(int i=2;i<num;i++)
{
if(num%i==0)
{
Count++;
break;
}
}
if(count==0)
cout<<”it is a prime number”;
else
3. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
cout<<”It is not prime number”;
getche();
}
********************output******************************
Enter the number to check: 97
It is a prime number
Enter the number to check: 8
It is not a prime number
_____________________________________________________________
____
4. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
Experiment 2
Aim:Writea program to read numberand to displaythe largest valuebetween
two numbers.
Source:
#include<iostream.h>
#include<conio.h>
using namespace std;
Class largest
{
Int d;
Public: void getdata();
Void display_large(largest,largest);
};
Void largest:: getdata()
{
cout<<”nnEnter value:”;
cin>>d;
}
Void largest::display_largest(largest o1,largest o2)
{
If(o1.d>o2.d)
cout<<”n object I contain largest value”;
else if (o1.d<o2.d)
cout<<”n object 2 contains largest value”;
else
cout<<”Bothare equals”;
}
5. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
void main()
{
largest o1,o2,o3;
clrscr();
01.getdata();
02.getdata();
03.display_largest(o1,o2);
getche();
}
****************************OUTOUT*************************
***************
Enter value: 12
Enter value: 22
Object 2 contains the largest value.
_____________________________________________________________
____
6. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
Experiment 3
Aim:Writea program to find sum of the first natural numbers:
1+2+3+………100byusing
(a) For loop
(b) While Loop
(c) DO….whileLoop
Source:
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
int ch,sum=0;
cout<<”Sumof first 100 natural numbers”;
cout<<”n 1. Using for loop”;
cout<<”n 2. Using while loop”;
cout<<”n 3. Using do….while loop”;
cout<<”Enter your choice:”;
cin>>ch;
switch(ch)
{
case 1: for(int num=1;num<=100;num++)
sum=num+sum;
7. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
cout<<”nn The sum of first 100 naturals number is”;
cin>>sum;
break;
case 2: while(num=100);
{
sum=num+sum;
num++;
}
cout<<”nn The sum of first 100 naturals number is”;
cin>>sum;
break;
case 3: do
{
sum=num+sum;
num++;
} while(num=100);
cout<<”nn The sum of first 100 naturals number is”;
cin>>sum;
break;
default: cout<<”please enter the valid choice”;
}
getche();
8. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
}
******************************OUTPUT*****************************************
****
Sum of first 100 natural numbers
1. Using for loop
2. Using while loop
3. Using do….while loop
Enter youe choice: 1
The sum of first natural number is :5050
______________________________________________________________________________
_____
9. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
Experiment 4
Aim:Writea program to find sum of the following series using function
declaration.
Sum=
Source:
#inlude<iostream.h>
#include<conio.h>
Using namespace std;
Void main()
{
long int fact(int); // function declaration……………………………
float power (float, int);
float sum,temp,x,pow;
int sign,I,n;
long int factval;
cout<<”Enter a value of n?”;
cin>>n;
cout<<”Enter the value of X?”;
cin>>x;
i=3;
sum=x;
sign=1;
while(i<=n)
10. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
{
factval=fact(i);
pow=power(x,i);
sign=(-1)*sign;
temp=sign*pow/factval;
sum=sum+temp;
i=i+2;
}
Cout<<”sumog the series is :”;
getche();
}
// end of the main function………………………………….
long int fact( int max)
{
long int value;
value=1;
for(int i=1;i<=max;++i)
value=value*I;
return(value);
}
float power(float x, int n)
{
float value2;
value2=1;
for(int j=1;j<=n;++j)
value2=value2*x;
return ( value2);
11. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
}
***************************OUTPUT**************************
*****
Enter a value for n?
7
Enter a value for x?
1
Sumof the series is: 0.841468
12. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
Experiment 5
Aim:Writea program to read the elementsof the given twomatricesof
order n*n and to perform the matrix multiplication.
Source:
#inlude<iostream.h>
#include<conio.h>
#define MAX 100
Using namespace std;
Void main()
{
void output(float a[max] [max], int n);
void mul(float a[max][ max], float b[max][ max],int n);
float a[max][max],b[max][max];
Int I,j,n;
cout<<”orderofmatrix A “<<endl;
cin>>n;
cout<<”enter the elements for the matrix A:”<<endl;
for(i=0;i<=n-1;++i)
{
for(j=0;j<=n-1;++j)
cin>>a[i][j];
}
cout<<”orderof matrix B “<<endl;
cin>>n;
cout<<”enter the elements for the matrix B:”<<endl;
for(i=0;i<=n-1;++i)
{
for(j=0;j<=n-1;++j)
cin>>b[i][j];
}
cout<<”outputA[i][j] “<<endl;
output(a,n);
13. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
cout<<”outputB[i][j] “<<endl;
output(b,n);
mul(a,b,n);
getche();
} // end of the main function…………………………….
// function definition………………………………………
void mul(float a[max][ max], float b[max][ max],int n)
{
void output(float c[max][ max],int n);
float c[max][ max];
int I,j,k;
for(i=0;i<=n-1;++i)
{
for(j=0;j<=n-1;++j)
{
c[i][j]=0.0;
for(k=0;k<=n-1;++k)
c[i][j]=c[i][j]+a[i][j]*b[k][j];
}
cout<<endl;
cout<<”Outputof c[i][j] matrix”<<endl;
output(c,n);
}
void output(float x[max][max],int n)
{
int I,j;
for (i=0; i<=n-1;++i)
{
for(j=0;j<=n-1;++j)
cout<<x[i][j]<<’t’;
cout<<endl;
}
}
14. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
****************************OUTPUT*************************
*****
Order of matrix A
3
Enter the elements of matrix A:
3 3 3
3 3 3
3 3 3
Order of matrix B
3
Enter the elements of matrix B:
3 3 3
3 3 3
3 3 3
Output A[i][j]
3 3 3
3 3 3
3 3 3
Output B[i][j]
3 3 3
3 3 3
3 3 3
Output of C[i][j] matrix
27 27 27
27 27 27
27 27 27
15. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
Experiment 6
Aim:Writea program to exchangethe contents of twovariablesby using
(a) call by value(b) call by reference.
Source:
(a)
// Call by value ………………
#inlude<iostream.h>
#include<conio.h>
Using namespace std;
Void main()
{
int x,y;
void swap(int,int);
x=100;
y=20;
cout<<”values before swap ()”<<endl;
cout<<”x = ”<< x <<”and y = ”<<y<<endl;
swap(x,y);
cout<<”values after swap swap ()”<<endl;
cout<<”x = ”<< x <<”and y = ”<<y<<endl;
getche();
}
// end of the main function………………………………
16. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
Void swap(int x,int y) // values will not be
swapped……………………..
{
Int temp;
Temp=x;
X=y;
Y=temp;
}
*************************output**************************
**********
Values before swap ()
X=100 and y==20
Values after swap ()
X=100 and y==20
(b) call by reference…………………………………
#inlude<iostream.h>
#include<conio.h>
Using namespace std;
Void main()
{
int x,y;
void swap(int *x,int *y);
x=100;
y=20;
cout<<”values before swap ()”<<endl;
cout<<”x = ”<< x <<”and y = ”<<y<<endl;
17. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
swap(x,y);
cout<<”values after swap swap ()”<<endl;
cout<<”x = ”<< x <<”and y = ”<<y<<endl;
getche();
} // end of the main function………………………………
Void swap(int *x, int * y) // values will be
swapped……………………..
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
*************************output**************************
**********
Values before swap ()
X=100 and y==20
Values after swap ()
X=20 and y==100
18. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
Experiment 7
Aim:Writea program to perform the following operationsof a complex
numberusinga structure.
1. Addition of two complex numbers
2. Subtraction of two complex numbers
3. Multiplication of twocomplex numbers
4. Division of two complex numbers
Source:
Struct complex {
float real;
float imag;
};
complex add (complex a,complex b);
complex sub (complex a,complex b);
complex mul (complex a,complex b);
complex div (complex a,complex b);
#inlude<iostream.h>
#include<conio.h>
using namespace std;
void main()
{
complex a,b,c;
int ch;
void menu(void);
cout<<”Enter the first complex number n”;
cin>>a.real>>a.imag;
cout<<”Enter the second complex number n”;
19. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
cin>>b.real>>b.imag;
menu();
while((ch = getchar()) !=’q’)
{
switch (ch)
{
case ‘a’:
c = add (a,b);
cout<<”Addition of two complex number n”;
cout<<c.real<<”+ i”<< c.imag << endl;
` break;
case ‘s’:
c = sub (a,b);
cout<<”Substractionof two complex number n”;
cout<<c.real<<”+ i”<< c.imag << endl;
` break;
case ‘m’:
c = mul (a,b);
cout<<”Multiplication of two complex number
n”;
cout<<c.real<<”+ i”<< c.imag << endl;
` break;
case ‘d’:
c = div (a,b);
cout<<”Division of two complex number n”;
cout<<c.real<<”+ i”<< c.imag << endl;
` break;
}
}
} // ……………..End ofthe main
function……………………………
void menu ()
20. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
{
cout<< “complex number operations nn”;
cout<<”menu()”;
cout<<”a -> Addition n”;
cout<<”s -> Substraction n”;
cout<<”m -> Multiplication n”;
cout<<”d -> Division n”;
cout<<”q -> Quit n ”;
cout<<” option please…………… n ”;
}
complex add (struct complex a, struct complex b)
{
complex c;
c.real=a.real+b.real;
c.imag=a.imag+b.imag;
return(c);
}
complex sub (struct complex a, struct complex b)
{
complex c;
c.real=a.real - b.real;
c.imag=a.imag + b.imag;
return(c);
}
complex mul (struct complex a, struct complex b)
{
complex c;
c.real=(a.real * b.real) – (a.imag * b.imag);
c.imag=(a.real * b.real) + (a.imag * b.imag);
return(c);
}
complex div (struct complex a, struct complex b)
{
21. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
complex c;
float temp;
temp = (b.real*b.real) + (b.imag*b.imag);
c.real=((a.real * b.real) + (a.imag * b.imag))/temp;
c.imag=((a.real * b.real) - (a.imag * b.imag))/temp;
return(c);
}
***********************OUTPUT*************************
**********
Enter the first complex number
1 1
Enter the second number
2 2
Complex number operations
Menu()
a -> Addition
s -> Substraction
m -> Multiplication
d -> Divison
q -> Quit or Exit?
Option, please ?
a
Additon of two complex numbers
3 + i3
s
Substraction of two complex numbers
-1i-1
m
Multiplication of two complex numbers
0i4
d
Division of two complex numbers
0.5i0
22. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
q
Experiment 8
Aim:Writea program to perform the swapping of twodata itemsof integers,
floating pointnumberand character type withthe help of function overloading.
Source:
#inlude<iostream.h>
#include<conio.h>
using namespace std;
void swap (int &ix, int &iy);
void swap (float &ix, float &iy);
void swap (char &ix, char &iy);
void main()
{
int ix,iy;
float fx, fy;
char cx,cy;
cout<<”enter the two integers n”<<endl;
cin>> ix>>iy;
cout<<”enter the two floating point numbers n”<<endl;
cin>> fx>>fy;
cout<<”enter the two characters n”<<endl;
23. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
cin>> cx>>cy;
cout<<”swappingof integers n”;
cout<<”ix = ” <<ix<< “iy = ”<<iy<<endl;
swap(ix,iy);
cout<<”After swapping ”<<endl;
cout<<”ix = ” <<ix<< “iy = ”<<iy<<endl;
cout<<endl;
cout<<”swapping of floating point numbers n”;
cout<<”fx = ” <<fx<< “fy = ”<<fy<<endl;
swap(fx,fy);
cout<<”After swapping ”<<endl;
cout<<”fx = ” <<fx<< “fy = ”<<fy<<endl;
cout<<endl;
cout<<”swapping of characters n”;
cout<<”cx= ” <<cx<< “cy= ”<<cy<<endl;
swap(cx,cy);
cout<<”After swapping ”<<endl;
cout<<”cx= ” <<cx<< “cy= ”<<cy<<endl;
cout<<endl;
} //……END OF THE main FUNCTION…………………………
void swap (int &a, int &b)
25. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
Enter any two integers
100 200
Enter any two floating point numbers
-11 22.22
Enter any two characters
s t
swapping of integers
ix = 100 iy = 200
After swapping
ix = 200 iy = 100
swapping of floating point numbers
fx = -11.11 fy = 22.219099
After swapping
fx = 22.219099 fy = -11.11
swapping of characters
cx = s cy = t
After swapping
cx = t cy = s
________________________________________________________
26. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
Experiment 9
Aim: Write a C++ program to to generate a fibonacciseries by
overloading:
(a)Prefix overloading (b) postfix overloading
Source:
(a)
#inlude<iostream.h>
#include<conio.h>
using namespace std;
class fibonnaci
{
private:
unsigned long int f0,f1,fib;
public:
fibonacci();
void operater++();
void display ();
};
fibonacci :: fibonacci()
{
fo=0;
27. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
f1=1;
fib=f0+f1;
}
void fibonacci :: display ()
{
cout<<fib<<’t’;
}
voiod fibonacci :: operator++( )
{
fo=f1;
f1=fib;
fib=f0+f1;
}
void main()
{
fibonacci obj;
int n;
cout<< “How many fibonacci numbers are to be ”;
cout<<”Displayed ? n”:
cin>>n;
for(int i=0; i<=n=1;++i)
28. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
{ obj.display();
++obj;
}
} ……………………..ENDof the main function
………………………………….
(b)
#inlude<iostream.h>
#include<conio.h>
using namespace std;
class fibonnaci
{
private:
unsigned long int f0,f1,fib;
public:
fibonacci(); //
………………..contructor………………………….
void operater++( int );
void display ();
};
fibonacci :: fibonacci()
{
29. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
fo=0;
f1=1;
fib=f0+f1;
}
void fibonacci :: display ()
{
cout<<fib<<’t’;
}
fibonacci fibonacci :: operator++(int x)
{
fo=f1;
f1=fib;
fib=f0+f1;
return *this;
}
void main()
{
fibonacci obj;
int n;
cout<< “How many fibonacci numbers are to be ”;
cout<<”Displayed ? n”:
cin>>n;
30. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
for(int i=0; i<=n=1;++i)
{ obj.display();
obj++; } } // …………………...End ofthe main()………………….
Experiment 10
Aim:Writea program to perform simplearithmeticoperationsof two
complex numbersusing operator overloading.
Source:
Struct complex {
float real;
float imag;
};
complex + (complex a,complex b);
complex - (complex a,complex b);
complex * (complex a,complex b);
complex / (complex a,complex b);
#inlude<iostream.h>
#include<conio.h>
using namespace std;
void main()
{
complex a,b,c;
int ch;
void menu(void);
cout<<”Enter the first complex number n”;
cin>>a.real>>a.imag;
cout<<”Enter the second complex number n”;
cin>>b.real>>b.imag;
31. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
menu();
while((ch = getchar()) !=’q’)
{
switch (ch)
{
case ‘a’:
c = add (a,b);
cout<<”Addition of two complex number n”;
cout<<c.real<<”+ i”<< c.imag << endl;
` break;
case ‘s’:
c = sub (a,b);
cout<<”Substractionof two complex number n”;
cout<<c.real<<”+ i”<< c.imag << endl;
` break;
case ‘m’:
c = mul (a,b);
cout<<”Multiplication of two complex number
n”;
cout<<c.real<<”+ i”<< c.imag << endl;
` break;
case ‘d’:
c = div (a,b);
cout<<”Division of two complex number n”;
cout<<c.real<<”+ i”<< c.imag << endl;
` break;
}
}
} // ……………..End ofthe main
function……………………………
void menu ()
{
32. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
cout<< “complex number operations nn”;
cout<<”menu()”;
cout<<”a -> Addition n”;
cout<<”s -> Substraction n”;
cout<<”m -> Multiplication n”;
cout<<”d -> Division n”;
cout<<”q -> Quit n ”;
cout<<” option please…………… n ”;
}
complex + (struct complex a, struct complex b)
{
complex c;
c.real=a.real+b.real;
c.imag=a.imag+b.imag;
return(c);
}
complex - (struct complex a, struct complex b)
{
complex c;
c.real=a.real - b.real;
c.imag=a.imag + b.imag;
return(c);
}
complex * (struct complex a, struct complex b)
{
complex c;
c.real=(a.real * b.real) – (a.imag * b.imag);
c.imag=(a.real * b.real) + (a.imag * b.imag);
return(c);
}
complex / (struct complex a, struct complex b)
{
complex c;
33. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
float temp;
temp = (b.real*b.real) + (b.imag*b.imag);
c.real=((a.real * b.real) + (a.imag * b.imag))/temp;
c.imag=((a.real * b.real) - (a.imag * b.imag))/temp;
return(c);
}
***********************OUTPUT*************************
**********
Enter the first complex number
1 1
Enter the second number
2 2
Complex number operations
Menu()
a -> Addition
s -> Substraction
m -> Multiplication
d -> Divison
q -> Quit or Exit?
Option, please ?
a
Additon of two complex numbers
3 + i3
s
Substraction of two complex numbers
-1i-1
m
Multiplication of two complex numbers
0i4
d
Division of two complex numbers
0.5i0
34. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha
q
_______________________________________________________
35. SHRI RAWATPURA SARKAR INSTITUTE OF TECHNOLOGY-II, NEW RAIPUR
CSE/4th
/CN Lab/PreparedbyVivekKumarSinha