0% found this document useful (0 votes)
51 views29 pages

II PU Lab Mannual 2023

Uploaded by

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

II PU Lab Mannual 2023

Uploaded by

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

II PU LAB MANNUAL

1. Write a C++ program to find the frequency present of an element in an array.


#include<iostream.h>
#include<conio.h>
class Frequency
{
private: int i,n,a[10],ele,count;

public: void read();


void cal();
void display();
};
void Frequency::read()
{
cout<<"Enter the size of the array"<<endl;
cin>>n;
cout<<"Enter the array elements"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter the element to find frequency"<<endl;
cin>>ele;
}
void Frequency::cal()
{
count=0;
for(i=0;i<n;i++)
{
if(a[i]==ele)
{
count++;
}
}
}
void Frequency::display()
{
if(count==0)
{
cout<<"Element doesnot exist";
}
else
{
cout<<"Frequency of element is="<<count;
}
}
void main()
{
Frequency f;
clrscr();
f.read();
f.cal();
f.display();
getch();
}

Output 1:
Enter the size of the array
5
Enter the array elements
10 20 30 20 40
Enter the element to find frequency
20
Frequency of element is=2

Output 2:
Enter the size of the array
5
Enter the array elements
10 20 30 40 50
Enter the element to find frequency
99
Element does not exist

2. Write a C++ program to insert an element into an array at a given position.


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class Insertion
{
private: int i,n,a[10],pos,ele;
public: void read();
void cal();
void display();
};
void Insertion::read()
{
cout<<"Enter the size of the array"<<endl;
cin>>n;
cout<<"Enter the array elements"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter the position to insert"<<endl;
cin>>pos;
cout<<"Enter the element to insert"<<endl;
cin>>ele;
}
void Insertion::cal()
{
if(pos>n)
{
cout<<"Array out of bond";
getch();
exit(0);
}
else
{
for(i=n-1;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=ele;
n=n+1;
}
}
void Insertion::display()
{
cout<<"Array elements after insertion"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
}
void main()
{
Insertion i;
clrscr();
i.read();
i.cal();
i.display();
getch();
}

Output 1:
Enter the size of the array
5
Enter the array elements
10 20 30 40 50
Enter the position to insert
2
Enter the element to insert
99
Array elements after insertion
10
20
99
30
40
50

Output 2:
Enter the size of the array
5
Enter the array elements
10 20 30 40 50
Enter the position to insert
7
Enter the element to insert
99
Array out of bond
3. Write a C++ program to delete an element from an array from a given position.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class Deletion
{
private: int i,n,a[10],pos;

public: void read();


void cal();
void display();
};
void Deletion::read()
{
cout<<"Enter the size of the array"<<endl;
cin>>n;
cout<<"Enter the array elements"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter the position to delete"<<endl;
cin>>pos;
}
void Deletion::cal()
{
if(pos>=n)
{
cout<<"Array out of bond";
getch();
exit(0);
}
else
{
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
}
}
void Deletion::display()
{
cout<<"Array elemets after deletion"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
}
void main()
{
Deletion d;
clrscr();
d.read();
d.cal();
d.display();
getch();
}

Output 1:
Enter the size of the array
5
Enter the array elements
10 20 30 40 50
Enter the position to delete
2
Array elemets after deletion
10
20
40
50

Output 2:
Enter the size of the array
5
Enter the array elements
10 20 30 40 50
Enter the position to delete
8
Array out of bond
4. Write a C++ program to sort the element of an array in ascending order using insertion
sort.
#include<iostream.h>
#include<conio.h>
class Sorting
{
private: int i,n,a[10],j,temp;

public: void read();


void cal();
void display();
};
void Sorting::read()
{
cout<<"Enter the size of the array"<<endl;
cin>>n;
cout<<"Enter the array elements"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
}
void Sorting::cal()
{
i=1;
while(i<n)
{
j=i;
while(j>=1)
{
if(a[j]<a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
j--;
}
i++;
}
}
void Sorting::display()
{
cout<<"Array elements after sorting"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
}
void main()
{
Sorting s;
clrscr();
s.read();
s.cal();
s.display();
getch();
}

Output:
Enter the size of the array
5
Enter the array elements
10 50 30 20 40
Array elements after sorting
10
20
30
40
50
5. Write a C++ program to search for a given element in an array using binary search
method.
#include<conio.h>
#include<iostream.h>
class Binary
{
private: int i,n,a[10],ele,low,high,mid,loc;

public: void read();


void cal();
void display();
};
void Binary::read()
{
cout<<"Enter the size of array"<<endl;
cin>>n;
cout<<"Enter the array elements"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter the element to search"<<endl;
cin>>ele;
}
void Binary::cal()
{
loc=-1;
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==ele)
{
loc=mid;
break;
}
else if(ele>a[mid])
{
low=mid+1;
}
else
{
high=mid-1;
}
}
}
void Binary::display()
{
if(loc==-1)
{
cout<<"Element doesnot exist";
}
else
{
cout<<"Element found at position"<<loc+1;
}
}
void main()
{
Binary b;
clrscr();
b.read();
b.cal();
b.display();
getch();
}

Output 1:
Enter the size of array
5
Enter the array elements
10 20 30 40 50
Enter the element to search
20
Element found at position 2

Output 2:
Enter the size of array
5
Enter the array elements
10 20 30 40 50
Enter the element to search
7
Element does not exist

6. Write a C++ program to create class to find simple interest.


#include<iostream.h>
#include<conio.h>
class Intrest
{
private: long float p,t,r,si,ta;
public: void read();
void cal();
void display();
};
void Intrest::read()
{
cout<<"Enter principle amount"<<endl;
cin>>p;
cout<<"Enter Rate of intrest"<<endl;
cin>>r;
cout<<"Enter time"<<endl;
cin>>t;
}
void Intrest::cal()
{
si=(p*t*r)/100;
ta=p+si;
}
void Intrest::display()
{
cout<<"Principle amount="<<p<<endl;
cout<<"Rate of intrest="<<r<<endl;
cout<<"Time="<<t<<endl;
cout<<"Simple intrest="<<si<<endl;
cout<<"Total amount="<<ta;
}
void main()
{
Intrest i;
clrscr();
i.read();
i.cal();
i.display();
getch();
}

Output:
Enter principle amount
1000
Enter Rate of intrest
20
Enter time
2
Principle amount=1000
Rate of intrest=20
Time=2
Simple intrest=400
Total amount=1400

7. Write a C++ program to find roots of quadratic equation.


#include<iostream.h>
#include<conio.h>
#include<math.h>
class Quadratic
{
private: float a,b,c,d,r1,r2;

public: void read();


void cal();
void display();
};
void Quadratic::read()
{
cout<<"Enter the value of a,b,c"<<endl;
cin>>a>>b>>c;
}
void Quadratic::cal()
{
d=(b*b)-(4*a*c);
display();
}
void Quadratic::display()
{
if(d==0)
{
r1=-b/(2*a);
cout<<"Root="<<r1<<endl;
}
else if(d>0)
{
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
cout<<"Root 1="<<r1<<endl;
cout<<"Root 2="<<r2<<endl;
}
else
{
cout<<"Imaginary Roots"<<endl;
}
}
void main()
{
Quadratic q;
clrscr();
q.read();
q.cal();
getch();
}

Output 1:
Enter the value of a,b,c
1 2 1
Root=-1

Output 2:
Enter the value of a,b,c
2 6 2
Root 1=-0.381966
Root 2=-2.618034

Output 3:
Enter the value of a,b,c
2 1 2
Imaginary Roots

8. Write a C++ program to illustrate function overloading.


#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
class Overloading
{
public: int area(int a)
{
return (a*a);
}

int area(int l, int b)


{
return (l*b);
}

int area(float a,float b,float c)


{
float s=(a+b+c)/2;
return (sqrt(s*(s-a)*(s-b)*(s-c)));
}
};
void main()
{
Overloading f;
int ch;
clrscr();
while(1)
{
cout<<"1: for Area of square"<<endl;
cout<<"2: for Area of rectangle"<<endl;
cout<<"3: for Area of Triangle"<<endl;
cout<<"4: for Exit"<<endl;
cin>>ch;
switch(ch)
{
case 1: cout<<"Area of square="<<f.area(10)<<endl;
break;
case 2: cout<<"Area of Rectangle="<<f.area(10,20)<<endl;
break;
case 3: cout<<"Area of Triangle="<<f.area(10.0,20.0,30.0)<<endl;
break;
case 4: cout<<"Program ended"<<endl;
getch();
exit(1);
default: cout<<"Invalid choice"<<endl;
}
getch();
}
}
Output:
1: for Area of square
2: for Area of rectangle
3: for Area of Triangle
4: for Exit
1
Area of square=100
1: for Area of square
2: for Area of rectangle
3: for Area of Triangle
4: for Exit
2
Area of Rectangle=200
1: for Area of square
2: for Area of rectangle
3: for Area of Triangle
4: for Exit
3
Area of Triangle=0
1: for Area of square
2: for Area of rectangle
3: for Area of Triangle
4: for Exit
4
Program ended

9. Write a C++ program to illustrate inline function.


#include<iostream.h>
#include<conio.h>
inline int cube(int a)
{
return (a*a*a);
}
void main()
{
int a;
clrscr();
cout<<"Enter the value of A"<<endl;
cin>>a;
cout<<"Cube of a number is="<<cube(a);
getch();
}

Output 1:
Enter the value of A
10
Cube of a number is= 1000

Output 2:
Enter the value of A
20
Cube of a number is= 8000

10. Write a C++ program to find sum series using constructor.


#include<iostream.h>
#include<conio.h>
#include<math.h>
class Series
{
private: int sum,x,n,i;

public: Series(int a, int b)


{
x=a;
n=b;
sum=1;
}
void cal();
void display();
};
void Series::cal()
{
for(i=1;i<=n;i++)
{
sum+=pow(x,i);
}
}
void Series::display()
{
cout<<"Sum of series="<<sum<<endl;
}
void main()
{
int a,b;
clrscr();
cout<<"Enter the base value"<<endl;
cin>>a;
cout<<"Enter the exponential value"<<endl;
cin>>b;
Series s(a,b);
s.cal();
s.display();
getch();
}

Output:
Enter the base value
2
Enter the exponential value
4
Sum of series=31

11. Write a C++ program to illustrate single inheritance.


#include<iostream.h>
#include<conio.h>
class Base
{
protected: int reg;
char name[20];

public: void readdata()


{
cout<<"Enter reg.no & Name"<<endl;
cin>>reg>>name;
}
void display()
{
cout<<"Reg. No="<<reg<<endl;
cout<<"Name="<<name<<endl;
}
};
class Derived:public Base
{
private: int m1,m2,tot;

public: void readmarks()


{
cout<<"Enter the 2 sub marks"<<endl;
cin>>m1>>m2;
}

void compute()
{
tot=m1+m2;
cout<<"Total marks="<<tot<<endl;
}
};
void main()
{
Derived d;
clrscr();
d.readdata();
d.readmarks();
d.display();
d.compute();
getch();
}

Output:
Enter reg.no & Name
123
Bapuji
Enter the 2 sub marks
90
80
Reg. No=123
<<Name=Bapuji
Total marks=170

12. Write a C++ program to illustrate pointer object.


#include<iostream.h>
#include<conio.h>
class Student
{
protected: int reg;
char name[20];

public: void read()


{
cout<<"Enter reg.no & Name"<<endl;
cin>>reg>>name;
}
void display()
{
cout<<"Reg. No="<<reg<<endl;
cout<<"Name="<<name<<endl;
}
};
void main()
{
Student *s;
clrscr();
s->read();
s->display();
getch();
}

Output:
Enter reg.no & Name
123
Bapuji
Reg. No=123
Name=Bapuji

13. Write a C++ program to perform push operation in stack.


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 3
class Stackop
{
private: int i,stack[MAX],top;
public: Stackop()
{
top=-1;
}
void push(int item)
{
if(top==MAX-1)
{
cout<<"Stack overflow"<<endl;
}
else
{
top=top+1;
stack[top]=item;
}
}

void display()
{
if(top==-1)
{
cout<<"Stack underflow"<<endl;
}
else
{
cout<<"Stack elemets are:"<<endl;
for(i=0;i<=top;i++)
{
cout<<stack[i]<<endl;
}
}
}
};
void main()
{
int ele,ch;
Stackop s;
clrscr();
while(1)
{
cout<<"1: for Push"<<endl;
cout<<"2: for Display"<<endl;
cout<<"3: for Exit"<<endl;
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter the element to insert"<<endl;
cin>>ele;
s.push(ele);
break;
case 2: s.display();
break;
case 3: cout<<"Program ended"<<endl;
getch();
exit(0);
default: cout<<"Inavlid choice"<<endl;
}
getch();
}
}

Output:
1: for Push
2: for Display
3: for Exit
1
Enter the element to insert
10
1: for Push
2: for Display
3: for Exit
1
Enter the element to insert
20
1: for Push
2: for Display
3: for Exit
1
Enter the element to insert
30
1: for Push
2: for Display
3: for Exit
1
Enter the element to insert
40
Stack overflow
1: for Push
2: for Display
3: for Exit
2
Stack elemets are:
10
20
30
1: for Push
2: for Display
3: for Exit
3
program ended

14. Write a C++ program to perform pop operation in stack.


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 3
class Stackop
{
private: int i,stack[MAX],top;

public: Stackop()
{
top=-1;
}
void push(int item)
{
if(top==MAX-1)
{
cout<<"Stack overflow"<<endl;
}
else
{
top=top+1;
stack[top]=item;
}
}
void pop()
{
if(top==-1)
{
cout<<"Stack Underflow"<<endl;
}
else
{
cout<<"Element popped="<<stack[top];
top=top-1;
}
}
void display()
{
if(top==-1)
{
cout<<"Stack underflow"<<endl;
}
else
{
cout<<"Stack elemets are:"<<endl;
for(i=0;i<=top;i++)
{
cout<<stack[i]<<endl;
}
}
}
};
void main()
{
int ele,ch;
Stackop s;
clrscr();
while(1)
{
cout<<"1; for Push"<<endl;
cout<<"2: for Pop"<<endl;
cout<<"3: for Display"<<endl;
cout<<"4: for Exit"<<endl;
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter the element to insert"<<endl;
cin>>ele;
s.push(ele);
break;
case 2: s.pop();
break;
case 3: s.display();
break;
case 4: cout<<"Program ended"<<endl;
getch();
exit(0);
default: cout<<"Inavlid choice"<<endl;
}
getch();
}
}

Output:
1: for Push
2: for Pop
3: for Display
4: for Exit
1
Enter the element to insert
10
1: for Push
2: for Pop
3: for Display
4: for Exit
1
Enter the element to insert
20
1: for Push
2: for Pop
3: for Display
4: for Exit
1
Enter the element to insert
3
1: for Push
2: for Pop
3: for Display
4: for Exit
1
Enter the element to insert
40
Stack overflow
1: for Push
2: for Pop
3: for Display
4: for Exit
3
Stack elemets are:
10
20
30
1: for Push
2: for Pop
3: for Display
4: for Exit
2
Element popped=30
1; for Push
2: for Pop
3: for Display
4: for Exit
2
Element popped=20
1; for Push
2: for Pop
3: for Display
4: for Exit
2
Element popped=10
1; for Push
2: for Pop
3: for Display
4: for Exit
2
Stack Underflow
1; for Push
2: for Pop
3: for Display
4: for Exit
4
Program ended

15. Write a C++ program to perform Enqueue and Dequeue operation.


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 3
class Queueop
{
private: int i,queue[MAX],front,rear;
public: Queueop()
{
front=0;
rear=-1;
}
void enqueue(int item)
{
if(rear==MAX-1)
{
cout<<"Queue overflow"<<endl;
}
else
{
rear=rear+1;
queue[rear]=item;
}
}
void dequeue()
{
if(rear==-1 || front>rear)
{
cout<<"Queue underflow"<<endl;
}
else
{
cout<<"Deleted item="<<queue[front]<<endl;
front=front+1;
}
}
void display()
{
if(rear==-1)
{
cout<<"Queue underflow"<<endl;
}
else
{
cout<<"Queue elemets are:"<<endl;
for(i=front;i<=rear;i++)
{
cout<<queue[i]<<endl;
}
}
}
};
void main()
{
int ele,ch;
clrscr();
Queueop q;
while(1)
{
cout<<"1: for Enqueue"<<endl;
cout<<"2: for Dequeue"<<endl;
cout<<"3: for Display"<<endl;
cout<<"4: for Exit"<<endl;
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter the element to insert"<<endl;
cin>>ele;
q.enqueue(ele);
break;
case 2: q.dequeue();
break;
case 3: q.display();
break;
case 4: cout<<"Program Ended"<<endl;
getch();
exit(0);
default: cout<<"Invalid choice"<<endl;
}
getch();
}
}
Output:
1: for Enqueue
2: for Dequeue
3: for Display
4: for Exit
1
Enter the element to insert
10
1: for Enqueue
2: for Dequeue
3: for Display
4: for Exit
1
Enter the element to insert
20
1: for Enqueue
2: for Dequeue
3: for Display
4: for Exit
1
Enter the element to insert
30
1: for Enqueue
2: for Dequeue
3: for Display
4: for Exit
1
Enter the element to insert
40
Queue overflow
1: for Enqueue
2: for Dequeue
3: for Display
4: for Exit
2
Deleted item=10
1: for Enqueue
2: for Dequeue
3: for Display
4: for Exit
2
Deleted item=20
1: for Enqueue
2: for Dequeue
3: for Display
4: for Exit
2
Deleted item=30
1: for Enqueue
2: for Dequeue
3: for Display
4: for Exit
2
Queue underflow
1: for Enqueue
2: for Dequeue
3: for Display
4: for Exit
4
Program Ended

You might also like