You are on page 1of 63

Page |1

EC6312 OOPS AND DATA STRUCTURES LABORATORY


LAB MANUAL
LIST OF EXPERIMENTS
1. Basic Programs for C++ Concepts
i.
ii.
iii.
iv.
v.
vi.
vii.
viii.
2.
3.
4.
5.

Area of a Circle Using Class


Find even or odd number Using Class
Print Fibonacci series using Constructors
Implementation of unary operator overloading
Implementation of Multiple inheritance
Find the mean value of a given number using friend function.
Implementation of Virtual function
To implement String Operations

Array implementation of List Abstract Data Type (ADT)


Linked list implementation of List ADT
Cursor implementation of List ADT
Stack ADT - Array and linked list implementations
The next two exercises are to be done by implementing the following source files
(a) Program source files for Stack Application 1
(b) Array implementation of Stack ADT
(c) Linked list implementation of Stack ADT
(d) Program source files for Stack Application 2
An appropriate header file for the Stack ADT should be #included in (a) and (d)

6.

Implement any Stack Application using array implementation of Stack ADT (by
implementing files (a) and (b) given above) and then using linked list implementation of
Stack ADT (by using files (a) and implementing file (c))
7. Queue ADT Array and linked list implementations8. Search Tree ADT - Binary Search Tree
9. Heap Sort
10. Quick Sort

Page |2
1. BASIC PROGRAMS
i.

AREA OF A CIRCLE USING CLASS

AIM:
To write a C++ program to find the area of a circle by implementing Classes and Objects.
PROCEDURE:
Step 1: Start the program
Step 2: Create a class circle with 2 data members.
Step 3: Create a member function area() to compute the area of the
Circle.
Step 4: Create a main() function and create an object for the class.
Step 5: Call the function area() by using the object.
Step 6: Stop the Program
PROGRAM:
#include<iostream.h>
#include<conio.h>
class circle
{
int r;
float ar;
public:
void area(int r)
{
ar=3.14*r*r;
cout<<"Area of the Circle is "<<ar;
}
};
void main()
{
circle obj;
//Object Creation
clrscr();
obj.area(2);
// Calling the function
getch();
}
OUTPUT:
Area of the circle is 12.56
RESULT:
Thus the program to find the area of a circle by implementing classes and objects was
written and executed.

Page |3
ii.

FIND EVEN OR ODD NUMBER USING CLASS

AIM:
To write a C++ program to find whether the given number is odd or even by
implementing Classes and Objects.
PROCEDURE:
Step 1: Start the program
Step 2: Create a class number a data member to read a number.
Step 3: Create a member function read() to read the input
Step 4: Create another member function find() to find whether the
given number is odd or even and print the result.
Step 5: Create a main() function and create an object for the class.
Step 6: Call the function read() and find() by using the object.
Step 7: Stop the Program
PROGRAM:
#include<iostream.h>
#include<conio.h>
class number
{
int num;
public:
void read()
{
cout<<"Enter a number
:
";
cin>>num;
}
void find()
{
if(num%2==0)
{
cout<<"The given number is Even";
}
else
{
cout<<"The given number is Odd";
}
}
};

Page |4
void main()
{
number obj;
clrscr();
obj.read();
obj.find();
getch();
}
OUTPUT 1:
Enter a Number
:
The Given number is Odd

457

OUTPUT 2:
Enter a Number
:
The Given number is Even

324

RESULT:
Thus the program to find whether the given number is odd or even by implementing
Classes and Objects was written and executed.

Page |5
iii.

FIBONACCI SERIES

AIM:
To write a C++ program to print Fibonacci Series by implementing constructors.
.
PROCEDURE:
Step 1: Start the program
Step 2: Create a class demo with 3 data members.
Step 3: Create a constructor demo for initializing the data members
Step 4: Create a member function fibo() to compute the Fibonacci series.
Step 5: Create a main() function and create an object for the class.
Step 6: Call the function fibo() by using the object.
Step 7: Stop the Program
PROGRAM:
#include<iostream.h>
#include<conio.h>
class demo
{
int a,b,t;
public:
demo()

// Constructor Definition
{
a=0;
b=1;
t=0;
}
void fibo(int n)
{
cout<<"\n\nFibonacci Series...\n\n";
if(n<2)
{
cout<<a;
}
else
{
cout<<a<<"\t"<<b;
for(int i=2;i<n;i++)
{

Page |6
t=a+b;
cout<<"\t"<<t;
a=b;
b=t;
}
}
}
};
void main()
{
demo obj;
int n;
clrscr();
cout<<"Enter No Of Terms
cin>>n;
obj.fibo(n);
getch();
}

";

OUTPUT:

RESULT:
Thus program to print Fibonacci Series by implementing constructors was written and
executed.

Page |7
iv.

UNARY OPERATOR OVERLOADING

AIM:
To write a program to increment and decrement the complex numbers using unary
operator overloading.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the class.
Step 3: Declare the variables and its member function.
Step 4: Using the function getvalue() to get the two numbers.
Step 5: Define the function operator ++ to increment the values
Step 6: Define the function operator - -to decrement the values.
Step 7: Define the display function.
Step 8: Declare the class object.
Step 9: Call the function getvalue
Step 10: Call the function operator ++() by incrementing the class object and call the function
display.
Step 11: Call the function operator - -() by decrementing the class object and call the function
display.
Step 12: Stop the program.

Page |8
PROGRAM:
#include<iostream.h>
#include<conio.h>

class complex
{
int a,b,c;
public:
complex(){}
void getvalue()
{
cout<<"Enter the Two Numbers:";
cin>>a>>b;
}
void operator++()
{
a=++a;
b=++b;
}
void operator--()
{
a=--a;
b=--b;

Page |9
}
void display()
{
cout<<a<<"+\t"<<b<<"i"<<endl;
}
};

void main()
{
clrscr();
complex obj;
obj.getvalue();
obj++;
cout<<"Increment Complex Number\n";
obj.display();
obj--;
cout<<"Decrement Complex Number\n";
obj.display();
getch();
}
OUTPUT:
Enter the two numbers: 3 6
Increment Complex Number

P a g e | 10
4+

7i

Decrement Complex Number


3

6i

RESULT:
Thus the program to increment and decrement the complex numbers using unary operator
overloading was written and executed.
v.

MULTIPLE INHERITANCE

AIM:
To write a program to find out the student details using multiple inheritance.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the base class student.
Step 3: Declare and define the function get() to get the student details.
Step 4: Declare the other class sports.
Step 5: Declare and define the function getsm() to read the sports mark.
Step 6: Create the class statement derived from student and sports.
Step 7: Declare and define the function display() to find out the total and average.
Step 8: Declare the derived class object,call the functions get(),getsm() and display().
Step 9: Stop the program.
PROGRAM:
#include<iostream.h>

P a g e | 11
#include<conio.h>
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks :";
cin>>m1>>m2;
}
};
class sports
{
protected:
int sm;
// sm = Sports mark
public:
void getsm()
{
cout<<"\nEnter the sports mark :";
cin>>sm;
}
};
class statement:public student,public sports
{
int tot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal
cout<<"\n\tAverage : "<<avg;
}
};
void main()
{
clrscr();
statement obj;
obj.get();
obj.getsm();
obj.display();

: "<<tot;

P a g e | 12
getch();
}
Output:
Enter the Roll no: 100
Enter two marks
90
80
Enter the Sports Mark: 90
Roll No: 100
Total : 260
Average: 86.66

RESULT:
Thus the program to find out the student details using multiple inheritance was written
and executed.
vi.

FRIEND FUNCTION

AIM:
To write a program to find the mean value of a given number using friend function.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class name as Base with data members and member functions.
STEP 3: The function get() is used to read the 2 inputs from the user.
STEP 4: Declare the friend function mean(base ob) inside the class.
STEP 5: Outside the class to define the friend function and do the following.
STEP 6: Return the mean value (ob.val1+ob.val2)/2 as a float.
STEP 7: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class base
{
int val1,val2;
public:

P a g e | 13
void get()
{
cout<<"Enter two values:";
cin>>val1>>val2;
}
friend float mean(base ob);
};
float mean(base ob)
{
return float(ob.val1+ob.val2)/2;
}
void main()
{
clrscr();
base obj;
obj.get();
cout<<"\n Mean value is : "<<mean(obj);
getch();
}
OUTPUT:
Enter two values: 10, 20
Mean Value is: 15
RESULT:
Thus the program to find the mean value of a given number using friend function
was written and executed.
vii.

VIRTUAL FUNCTIONS

AIM
To write a program for virtual functions.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the base class base.
Step 3: Declare and define the virtual function show().
Step 4: Declare and define the function display().
Step 5: Create the derived class from the base class.
Step 6: Declare and define the functions display() and show().
Step 7: Create the base class object and pointer variable.
Step 8: Call the functions display() and show() using the base class object and pointer.

P a g e | 14
Step 9: Create the derived class object and call the functions display() and show() using the
derived class object and pointer.
Step 10: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void show()
{
cout<<"\n Base class show:";
}
void display()
{
cout<<"\n Base class display:" ;
}
};
class drive:public base
{
public:
void display()
{
cout<<"\n Drive class display:";
}
void show()
{
cout<<"\n Drive class show:";
}
};
void main()
{
clrscr();
base obj1;
base *p;
cout<<"\n\t P points to base:\n" ;
p=&obj1;
p->display();
p->show();
cout<<"\n\n\t P points to drive:\n";

P a g e | 15
drive obj2;
p=&obj2;
p->display();
p->show();
getch();
}
OUTPUT:
P points to Base
Base class display
Base class show
P points to Drive
Base class Display
Drive class Show

RESULT:
Thus the program for virtual functions was written and executed.
viii. STRING OPERATIONS
AIM:
To write a C++ program to implement String Operations
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the string a and assign HELLO to that.
Step 3: Declare the string a and assign welcome to that.
Step 4: The function strlwr converts the string into lowercase letters.
Step 5: The function strupr converts the string into uppercase letters.
Step 6: The function strcat concatenates two strings into one string.
Step 7: The function strrev reverses the symbols of the string.

P a g e | 16
Step 8: The function strcpy copies one string into another string.
Step 9: Stop the program.
PROGRAM:
#include<iostream.h>
#include<string.h>
#include<conio.h>
void main()
{
char a[20]="HELLO",b[20]="welcome",c[20];
int i,j,m,n;
clrscr();
cout<<"\n\nString 1 =
"<<a;
cout<<"\n\nString 2 =
"<<b;
cout<<"\n\nLower Case of String 1 is "<<strlwr(a);
cout<<"\n\nUpper Case of String 2 is "<<strupr(b);
cout<<"\n\nConcatination of 2 Strings is "<<strcat(a,b);
cout<<"\n\nReverse of String 2 is "<<strrev(b);
cout<<"\n\nCopy of String 1 in String 2 is "<<strcpy(b,a);
getch();
}

OUTPUT:

RESULT:
Thus the program to implement String Operations was created and executed.

P a g e | 17

2. ARRAY IMPLEMENTATION OF LIST ABSTRACT DATA TYPE (ADT)


AIM:To write a C++ program to implement the list operations using array.
ALGORITHM:
1. Initialize the list with n items.
2. Get the choice of operation either insertion or deletion.
3. For insertion, get the item to be inserted and its position. Then position to last position move
the items in the list one step forward, then insert the new item in its corresponding position.
4. For deletion, get the item to be deleted,then find its position and move the items in the list one
step backward from last position to position found.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

P a g e | 18
int a[20],n;
void main()
{
int i,ch,p,v;
clrscr();
cout<<"\n Enter the no. of elements in the list : ";
cin>>n;
cout<<"\n Enter the List of Element one by one : ";
for (i=0;i<n;i++)
cin>>a[i];
do
{
cout<<"\n 1. Insert.... 2.Delete... 3.Display... 4.Exit... :";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n Enter the Position & Element : ";
cin>>p>>v;
for(i=n;i>=p;i--)
a[i]=a[i-1];
a[p-1]=v;
n++;
break;
case 2:
cout<<"\n Enter the Deleting Element : ";
cin>>v;
p=0;
for(i=0;i<n;i++)
{
if(a[i]==v)
p=1;
a[i]=a[i+p];
}
if(p==0)
cout<<"\n The element is not in the list";
else
n--;
break;
case 3:
cout<<"\n The List of Elements : \n";
for(i=0;i<n;i++)
cout<<" "<<a[i]<<" ";
break;
case 4:
exit(0);

P a g e | 19
}
}
while(ch<=4);
getch();
}
OUTPUT:
Enter the no. of elements in the list : 5
Enter the List of Element one by one : 10 20 30 40 50
1. Insert.... 2.Delete... 3.Display... 4.Exit... :1
Enter the Position & Element : 2 25
1. Insert.... 2.Delete... 3.Display... 4.Exit... :3
The List of Elements :
10

25

20

30

40

50

1. Insert.... 2.Delete... 3.Display... 4.Exit... :2


Enter the Deleting Element : 40
1. Insert.... 2.Delete... 3.Display... 4.Exit... :3
The List of Elements :
10

25

20

30

50

1. Insert.... 2.Delete... 3.Display... 4.Exit... :4


RESULT:
Thus the program to implement the list operations using array was created and executed.
3. LINKED LIST IMPLEMENTATION OF LIST ADT
AIM:
To write a C++ program to implement the list operations using linked list.
ALGORITHM:
Step 1:- Define a list as a node of a structure with one DATA and one LINK pointing to the next
element in the structure.
Step 2:- Declare the function prototypes create( ),insert( ),delete( ) and display( ) to perform the
list functions.
Step 3:- Declare necessary pointer variables as struct node data type.
Step 4:- Get a choice from the user. If the choice is create, Get first data from the user by calling

P a g e | 20
the function create( ), and display contents of the list.
Step 5:- If the choice is insert, Get data and its position by calling the function insert( ), and
assign the LINK field of the given data node according to the position. Display the
contents of the list.
Step 6:- If the choice is delete, get the position of the data which is going to be removed from the
list and assign LINK field of the previous node to the address of the next node.
Step 7:- Repeat the steps 4, 5 & 6 until the choice is exit.
Step 8:- Terminate the execution.
PROGRAM:
# include <iostream.h>
# include <conio.h>
# include <alloc.h>
#include<stdlib.h>
typedef struct node
{
int info;
struct node *link;
} nodeType;
nodeType *start;
void create_list(int data)
{
nodeType *q, *tmp;
tmp =(nodeType *)malloc(sizeof(nodeType));
tmp->info = data;
tmp->link = NULL;
if(start == NULL) /*If list is empty */
start = tmp;
else
{
/*Element inserted at the end */
q=start;
while(q->link != NULL)
q = q->link;
q->link = tmp;
}
}/*End of create_list()*/
void addatbeg(int data)
{
nodeType *tmp;
tmp =(nodeType *)malloc(sizeof(nodeType));
tmp->info = data;
tmp->link = start;

P a g e | 21
start = tmp;
}/*End of addatbeg()*/
void addafter(int data, int pos)
{
nodeType *tmp, *q;
int i;
q = start;
for(i = 0; i < pos-1; i++)
{
q = q->link;
if(q == NULL)
{
cout<<"\nThere are less than %d elements";
return;
}
}/*End of for*/
tmp =(nodeType *)malloc(sizeof(nodeType));
tmp->link = q->link;
tmp->info = data;
q->link = tmp;
}/*End of addafter()*/
void del(int data)
{
nodeType *tmp, *q;
if(start->info == data)
{
tmp = start;
start = start->link; /*First element deleted*/
free(tmp);
return;
}
q = start;
while(q->link->link != NULL)
{
if(q->link->info == data) /*Element deleted in between*/
{
tmp = q->link;
q->link = tmp->link;
free(tmp);
return;
}
q = q->link;
}/*End of while */
if(q->link->info == data) /*Last element deleted*/
{
tmp = q->link;

P a g e | 22
free(tmp);
q->link = NULL;
return;
}
cout<<"\nElement %d not found\n";
}/*End of del()*/
void display()
{
nodeType *q;
if(start == NULL)
{
cout<<"\nList is empty\n";
return;
}
q = start;
cout<<"\nList is :";
while(q != NULL)
{
cout<<" "<<q->info<<" ";
q = q->link;
}
cout<<"\n";
}/*End of display() */
int main()
{
int choice, n, m, position, i;
clrscr();
start = NULL;
do
{
cout<<"\n1. Create List\n";
cout<<"2. Add at begining\n";
cout<<"3. Add after \n";
cout<<"4. Delete\n";
cout<<"5. Display\n";
cout<<"6. Quit\n";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\nHow many nodes you want : ";
cin>>n;
cout<<"Enter the elements : ";
for(i = 0; i < n; i++)
{

P a g e | 23
cin>>m;
create_list(m);
}
break;
case 2:
cout<<"\nEnter the element : ";
cin>>m;
addatbeg(m);
break;
case 3:
cout<<"\nEnter the element : ";
cin>>m;
cout<<"Enter the position after which this element is inserted : ";
cin>>position;
addafter(m, position);
break;
case 4:
if(start == NULL)
{
cout<<"\nList is empty\n";
continue;
}
cout<<"\nEnter the element for deletion : ";
cin>>m;
del(m);
break;
case 5:
display();
break;
case 6:
exit(1);
default:
cout<<"\nWrong choice\n";
}/*End of switch */
}while(choice<=6);
return 0;
}/*End of main()*/
OUTPUT :
1. Create List
2. Add at begining
3. Add after
4. Delete
5. Display
6. Quit
Enter your choice : 1

P a g e | 24
How many nodes you want : 5
Enter the elements : 10 20 30 40 50
1. Create List
2. Add at begining
3. Add after
4. Delete
5. Display
6. Quit
Enter your choice : 2
Enter the element : 5
1. Create List
2. Add at begining
3. Add after
4. Delete
5. Display
6. Quit
Enter your choice : 5
List is : 5 10 20 30 40 50
1. Create List
2. Add at begining
3. Add after
4. Delete
5. Display
6. Quit
Enter your choice : 3
Enter the element : 25
Enter the position after which this element is inserted : 3
1. Create List
2. Add at begining
3. Add after
4. Delete
5. Display
6. Quit
Enter your choice : 5
List is : 5 10 20 25 30 40 50
1. Create List
2. Add at begining
3. Add after

P a g e | 25
4. Delete
5. Display
6. Quit
Enter your choice : 4
Enter the element for deletion : 30
1. Create List
2. Add at begining
3. Add after
4. Delete
5. Display
6. Quit
Enter your choice : 5
List is : 5 10 20 25 40 50
1. Create List
2. Add at begining
3. Add after
4. Delete
5. Display
6. Quit
Enter your choice : 6
RESULT:
Thus the program to implement the list operations using linked list was created and
executed.
4. CURSOR IMPLEMENTATION OF LIST ADT
AIM:
To write a C++ program to implement the list using cursor.
ALGORITHM:
1. Start the program
2. Create a menu in main program
3. Get the choice
4. The choice is one create a new
5. The choice is two then insertion.
6. The choice is three delete an element.
7. The choice is four display the elements.

P a g e | 26
8. The choice is five search
9. The choice six exit
10. Stop
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
typedef int ElementType;
#define SpaceSize 100
#define NIL 0
typedef int PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
struct Node
{
ElementType Element;
Position Next;
};
struct Node CursorSpace[ SpaceSize ];
static Position CursorAlloc( void )
{
Position P;
P = CursorSpace[ 0 ].Next;
CursorSpace[ 0 ].Next = CursorSpace[ P ].Next;
return P;
}
static void CursorFree( Position P )
{
CursorSpace[ P ].Next = CursorSpace[ 0 ].Next;
CursorSpace[ 0 ].Next = P;
}
void InitializeCursorSpace( void )
{
int i;

P a g e | 27
for( i = 0; i < SpaceSize; i++ )
CursorSpace[ i ].Next = i + 1;
CursorSpace[ SpaceSize - 1 ].Next = 0;
}
void DeleteList( List L )
{
Position P, Tmp;
P = CursorSpace[ L ].Next;
CursorSpace[ L ].Next = 0;
while( P != 0 )
{
Tmp = CursorSpace[ P ].Next;
CursorFree( P );
P = Tmp;
}
}
List MakeEmpty( List L )
{
if( L != NIL )
DeleteList( L );
L = CursorAlloc( );
if( L == 0 )
printf( "\nOut of memory!" );
CursorSpace[ L ].Next = 0;
return L;
}
int IsEmpty( List L )
{
return CursorSpace[ L ].Next == 0;
}
int IsLast( Position P, List L )
{
return CursorSpace[ P ].Next == 0;
}
Position Find( ElementType X, List L )
{
Position P;
P = CursorSpace[ L ].Next;
while( P && CursorSpace[ P ].Element != X )

P a g e | 28
P = CursorSpace[ P ].Next;
return P;
}
void Delete( ElementType X, List L )
{
Position P, TmpCell;
P = FindPrevious( X, L );
if( !IsLast( P, L ) )
{
TmpCell = CursorSpace[ P ].Next;
CursorSpace[ P ].Next = CursorSpace[ TmpCell ].Next;
CursorFree( TmpCell );
}
}
Position FindPrevious( ElementType X, List L )
{
Position P;
P = L;
while( CursorSpace[ P ].Next &&
CursorSpace[ CursorSpace[ P ].Next ].Element != X )
P = CursorSpace[ P ].Next;
return P;
}
void Insert( ElementType X, List L, Position P )
{
Position TmpCell;
TmpCell = CursorAlloc( );
if( TmpCell == 0 )
printf( "\nOut of space!!!" );
CursorSpace[ TmpCell ].Element = X;
CursorSpace[ TmpCell ].Next = CursorSpace[ P ].Next;
CursorSpace[ P ].Next = TmpCell;
}
Position Header( List L )
{
return L;
}

P a g e | 29
Position First( List L )
{
return CursorSpace[ L ].Next;
}
Position Advance( Position P )
{
return CursorSpace[ P ].Next;
}
ElementType Retrieve( Position P )
{
return CursorSpace[ P ].Element;
}
void PrintList( const List L )
{
Position P = Header( L );
if( IsEmpty( L ) )
printf( "\nEmpty list\n" );
else
{
printf("\n");
do
{
P = Advance( P );
printf( "%d ", Retrieve( P ) );
}
while( !IsLast( P, L ) );
printf( "\n" );
}
}
int main( )
{
List L;
Position P;
int i;
InitializeCursorSpace( );
L = MakeEmpty( NIL );
P = Header( L );
PrintList( L );
for( i = 0; i < 15; i++ )

P a g e | 30
{
Insert( i, L, P );
PrintList( L );
P = Advance( P );
}
for( i = 0; i < 15; i += 2 )
Delete( i, L );
printf("\n");
for( i = 0; i < 15; i++ )
if( ( i % 2 == 0 ) == ( Find( i, L ) != NIL ) )
printf( "Find %d Fails\n", i);
PrintList( L );
DeleteList( L );
getch();
return 0;
}
OUTPUT :
Empty list
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9 10 11
0 1 2 3 4 5 6 7 8 9 10 11 12

P a g e | 31

0 1 2 3 4 5 6 7 8 9 10 11 12 13
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 3 5 7 9 11 13

RESULT:
Thus the cursor implementation using list has been successfully executed.

5.a. STACK ADT USING ARRAY IMPLEMENTATION


AIM:
To write a C++ program to implement stack using array.
ALGORITHM:
1. Declare an array size SIZE.
2. Assign top as a pointer to denote the top element in the stack
3. Get the new element item to be added in to the stack.
4. If top is greater than or equal to SIZE then display stack over flow; otherwise set top=top+1.
5. Set Stack [top] = item.
6. To delete top element from the stack check if top =0,then display stack underflow, otherwise
decrement top by one, and display S [top+1].

P a g e | 32
7. Display the stack from top to 1.
PROGRAM :
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define SIZE 3
int top = -1;
int stack[SIZE];
void push()
{
int item;
if(top == (SIZE-1))
cout<<"\nStack Overflow\n";
else
{
cout<<"\nEnter the item to be pushed in stack : ";
cin>>item;
top = top+1;
stack[top] = item;
}
}/*End of push()*/
void pop()
{
if(top == -1)
cout<<"\nStack Underflow\n";
else
{
cout<<"\nPopped element is :"<<stack[top];
top = top-1;
}
}/*End of pop()*/
void display()
{
int i;
if(top == -1)
cout<<"\nStack is empty\n";
else
{
cout<<"\nStack elements :";
for(i = top; i >=0; i--)
cout<<" "<<stack[i];
cout<<"\n";
}
}/*End of display()*/
void main()
{

P a g e | 33
int choice;
clrscr();
do
{
cout<<"\n1.Push\n";
cout<<"2.Pop\n";
cout<<"3.Display\n";
cout<<"4.Quit\n";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1 :
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
cout<<"Wrong choice\n";
}/*End of switch*/
}while(choice<=4);
getch();
}/*End of main()*/
Output :
1.Push
2.Pop
3.Display
4.Quit
Enter your choice : 1
Enter the item to be pushed in stack : 10
1.Push
2.Pop
3.Display
4.Quit
Enter your choice : 1
Enter the item to be pushed in stack : 20

P a g e | 34

1.Push
2.Pop
3.Display
4.Quit
Enter your choice : 1
Enter the item to be pushed in stack : 30
1.Push
2.Pop
3.Display
4.Quit
Enter your choice : 1
Stack Overflow
1.Push
2.Pop
3.Display
4.Quit
Enter your choice : 3
Stack elements : 30 20 10
1.Push
2.Pop
3.Display
4.Quit
Enter your choice : 2
Popped element is : 30
1.Push
2.Pop
3.Display
4.Quit
Enter your choice : 2
Popped element is : 20
1.Push
2.Pop
3.Display
4.Quit
Enter your choice : 2

P a g e | 35

Popped element is : 10
1.Push
2.Pop
3.Display
4.Quit
Enter your choice : 2
Stack Underflow
1.Push
2.Pop
3.Display
4.Quit
Enter your choice : 4
RESULT:
Thus the Stack ADT using array implementation has been successfully executed.

5.b. STACK ADT USING LINKED LIST IMPLEMENTATION


AIM:
To write a C++ program to implement a stack using Linked list.
ALGORITHM:
1. Start the program
2. Create a menu in main program
3. Get the choice
4. If the choice is one Push
5. If choice is two Pop
6. If the choice is three Display list
7. Else come out of the menu program
8. Stop
PROGRAM :
#include<iostream.h>
#include<stdlib.h>

P a g e | 36
#include<conio.h>
void push(int);
void pop();
void display();
struct linked
{
int num;
struct linked *link;
}*top = NULL, *p, *q;
void main( )
{
int ch, tnum, flag=0;
clrscr();
do
{
cout<<"\n MENU \n 1.Push \t 2.Pop \t 3.Display \t 4. Exit \n Enter your choice:";
cin>>ch;
switch( ch )
{
case 1:
cout<<"\n Enter the element:";
cin>>tnum;
for( q = top ; q != NULL ; q = q->link )
{
if(tnum==q->num)
flag=1;
}
if ( flag==0)
push( tnum );
else
cout<<"\n Element already exists";
break;
case 2 :
pop( );
break;
case 3 :
display( );
break;
case 4 : break;
}
}while(ch < 4);
}
void push( int tnum)
{
p = (struct linked*) malloc ( sizeof ( struct linked) );
p->num = tnum;

P a g e | 37
p->link = NULL;
if ( top==NULL)
top = p;
else
{
p->link = top;
top= p;
}
display( );
}
void pop( )
{
cout<<"\n POP Function \n";
if( top==NULL)
{
cout<<"\n Stack is empty";
return;
}
else
top = top->link;
}
void display( )
{
cout<<"\n The New list is :\n";
for( q = top ; q != NULL ; q = q->link )
{
cout<<" "<<q->num;
}
}
OUTPUT :
MENU
1.Push
2.Pop
Enter your choice:1

3.Display

4. Exit

3.Display

4. Exit

Enter the element:10


The New list is :
10
MENU
1.Push
2.Pop
Enter your choice:1
Enter the element:20

P a g e | 38
The New list is :
20
10
MENU
1.Push
2.Pop
Enter your choice:1

3.Display

4. Exit

3.Display

4. Exit

3.Display

4. Exit

3.Display

4. Exit

3.Display

4. Exit

3.Display

4. Exit

Enter the element:30


The New list is :
30
20
10
MENU
1.Push
2.Pop
Enter your choice:2
POP Function
MENU
1.Push
2.Pop
Enter your choice:3
The New list is :
20
10
MENU
1.Push
2.Pop
Enter your choice:2
POP Function
MENU
1.Push
2.Pop
Enter your choice:3
The New list is :
10
MENU
1.Push
2.Pop
Enter your choice:2

P a g e | 39
POP Function
MENU
1.Push
2.Pop
Enter your choice:3

3.Display

4. Exit

3.Display

4. Exit

3.Display

4. Exit

The New list is :


MENU
1.Push
2.Pop
Enter your choice:2
POP Function
Stack Underflow
MENU
1.Push
2.Pop
Enter your choice:4

RESULT:
Thus the Stack ADT using linked list implementation has been successfully executed.

6a.STACK APPLICATION -Balanced Parenthesis


AIM:
To write a C program for checking Balanced Parenthesis in an expression.
ALGORITHM:
1. Start the program.
2. Enter the expression.
3. Read the symbols of the expression one by one.
4. If it is (, then push it into the stack.
5. If it is ), then pop one ( off the stack.
6. After reading all the symbols of the expression, if the stack is empty, then display Balanced
Paranthesis.
7. Otherwise display Unbalanced Paranthesis
PROGRAM:

P a g e | 40

#include<iostream.h>
#include<conio.h>
#include<string.h>
char stack[20];
int top=0;
void push(char);
void pop();
void main( )
{
char s[10];
int i,len;
clrscr();
cout<<"\n Enter the expression:";
cin>>s;
len = strlen(s);
for(i=0;i<len;i++)
{
if(s[i]=='(')
{
top++;
push(s[i]);
}
if(s[i]==')')
pop( );
}
if(top == 0)
cout<<"\n Balanced parenthesis";
else
cout<<"\n Unbalanced parenthesis";
getch();
}
void push(char a)
{
stack[top]=a;
}
void pop( )
{
top--;
}
OUTPUT:
Enter the expression: (a+b)*(c+d)
Balanced Parenthesis
Enter the expression: ((x*y)/(z*x)
Unbalanced Parenthesis

P a g e | 41

RESULT:
Thus the C++ program for checking of balanced paranthesis has been successfully
executed.

6a.STACK APPLICATION -Evaluation of postfix expression


AIM:
To write a C++ program for evaluating postfix expression.
ALGORITHM:
1. Start the program.
2. Enter the postfix expression.
3. Read the postfix expression one character at a time.
4. If the character is an operand, push it associated value onto the stack.
5. If the character is an operator, pop two values from the stack, apply the operator to them and
push the result onto the stack.
6. Display the value
PROGRAM:
#include<iostream.h>

P a g e | 42
#include<conio.h>
#include<stdlib.h>
#include<string.h>
int calc(int,int,char);
int value(char);
int stack[20];
int evalpost(char[]);
void push(int);
int pop();
int top = 0;
void main( )
{
char a[20];
int i,res;
clrscr();
cout<<"\nEnter the postfix expression:";
cin>>a;
res = evalpost(a);
cout<<"\n The result is "<<res;
getch();
}
int evalpost(char postexp[ ])
{
int i=0,s,op1,op2;
char ch;
for(;i<strlen(postexp);i++)
{
if(postexp[i]!='\0')
{
ch = postexp[i];
if((ch>='a')&&(ch<='z'))
push(value(ch));
if((ch=='+')||(ch=='-')||(ch=='*')||(ch=='/'))
{
op2=pop( );
op1=pop( );
push(calc(op1,op2,ch));
}
}
}
s=pop( );
return(s);
}
int calc(int a,int b,char c)
{
switch(c)

P a g e | 43
{
case '+':return(a+b);
case '-':return(a-b);
case '*':return(a*b);
case '/':return(a/b);
}
}
int value(char ch)
{
int val;
cout<<"\n Enter value of "<<ch;
cin>>val;
return(val);
}
void push(int a)
{
top++;
stack[top]=a;
}
int pop( )
{
int value;
value =stack[top];
top--;
return value;
}

OUTPUT:
Enter the postfix expression: ab+cd-*
Enter value of a: 3
Enter value of b: 4
Enter value of c: 8
Enter value of d: 4
The result is 28
RESULT:
Thus the evaluation of postfix expression has been successfully executed.

P a g e | 44

7.a. QUEUE ADT ARRAY IMPLEMENTATIONS


AIM:
To write a C++ program to do the Queue operations using array implementations.
ALGORITHM:
1.
2.
3.
4.
5.
6.

Declare the queue size.


Assign front and rear as zero.
Get the new element item to be added in to the queue.
Insert the element at the rear end and increment the rear.
Delete the element from the front end and increment the front.
Display the queue from front to top.

PROGRAM :
#include<iostream.h>
#include<conio.h>

P a g e | 45
#include<stdlib.h>
int a[20];
void main( )
{
int i,l,ch,ele,front,rear,size;
clrscr();
front =rear=0;
cout<<"\n Enter the size of queue:";
cin>>size;
do
{
cout<<"\n1.Insert\n2.Delete\n3.Display\n4.Exit\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
if(rear==size)
cout<<"\nQueue overflow";
else
{
cout<<"\nEnter the element:";
cin>>ele;
if(front==rear)
{
a[front]=a[rear]=ele;
rear=rear+1;
}
else
{
a[rear]=ele;
rear=rear+1;
}
}
break;
case 2:
if(front==rear)
{
cout<<"\n Queue underflow";
front =rear=0;
}
else
{
cout<<"\n Deleted element is "<<a[front];
front =front+1;
}
break;

P a g e | 46
case 3:
if(front==rear)
{
cout<<"\n Queue is empty";
front =rear=0;
}
else
{
for(i=front;i<rear;i++)
{
cout<<" "<<a[i];
}
}
break;
case 4:
exit(0);
}
}while(ch<5);
}
OUTPUT:
Enter the size of queue:3
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the element:1
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the element:2
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:1
Enter the element:3
1.Insert

P a g e | 47
2.Delete
3.Display
4.Exit
Enter your choice:1
Queue overflow
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:3
1
2
3
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:2
Deleted element is 1
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:3
2
3
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:2
Deleted element is 2
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:2

P a g e | 48
Deleted element is 3
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:2
Queue underflow
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:3
Queue is empty
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice:4
RESULT:
Thus the queue ADT using array implementation has been successfully executed.
7.b. QUEUE ADT LINKED LIST IMPLEMENTATIONS
AIM:
To write a C++ program to do the Queue operations using linked list implementations.
ALGORITHM:
1.
2.
3.
4.
5.
6.

Declare the node queue with structure members num and link.
Assign head as NULL.
Get the new element item to be added in to the queue.
Insert the element at the rear end.
Delete the element from the front end.
Display the queue from front to top.

PROGRAM:
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>

P a g e | 49
struct queue
{
int num;
struct queue *link;
}*p,*q,*head=NULL;
void insert(int);
void del();
void display();
void main( )
{
int ch,tnum;
clrscr();
do
{
cout<<"\n1.Insert\n2.Delete\n3.Display\n4.Exit\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter number to be inserted:";
cin>>tnum;
insert(tnum);
display( );
break;
case 2:
cout<<"\nDeleted number is "<<head->num;
del( );
break;
case 3:
display( );
break;
case 4:
break;
}
}while(ch<4);
getch();
}
void insert(int tnum)
{
int true=0;
for(q=head;q!=NULL;q=q->link)
if(q->num==tnum)
{
cout<<"\nNumber already exists";
true=1;
}

P a g e | 50
if(true!=1)
{
p=(struct queue*)malloc(sizeof(struct queue));
p->num=tnum;
p->link=NULL;
if(head==NULL)
{
head=p;
}
else
{
q=head;
while(q->link!=NULL)
{
q=q->link;
}
q->link=p;
}
}
}
void del( )
{
if(head==NULL)
cout<<"\nQueue is empty";
else
{
if(head->link==NULL)
head=NULL;
else
head=head->link;
}
}
void display( )
{
if(head==NULL)
{
cout<<"\nQueue is empty";
return;
}
else
for(q=head;q!=NULL;q=q->link)
cout<<" "<<q->num;
}
OUTPUT:
1. Insert
2. Delete

P a g e | 51
3. Display
4. Exit
Enter your choice: 1
Enter the number to be inserted: 1
1
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the number to be inserted: 2
1
2
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the number to be inserted: 3
1
2
3
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 2
Deleted number is 1
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
2
3
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 4
RESULT:
Thus the queue ADT using linked list implementation has been successfully executed.

P a g e | 52

8. SEARCH TREE ADT-BINARY SEARCH TREE


AIM:
To write a C program for implementing Binary search tree.
ALGORITHM:
1. Start the program
2. Select the menu choice
3. If the choice is 1, create a node
4. If the choice is 2, insert a node to binary tree
5. If the choice is 3, delete the node from binary tree
6. If the choice is 4, find the node in binary tree
7. If the choice is 5, find the minimum node in binary tree
8. If the choice is 6, find the maximum node in binary tree
9. If the choice is 7, it exits the program
8. Stop the program.
PROGRAM:
#include<iostream.h>

P a g e | 53
#include<conio.h>
#include<process.h>
#include<alloc.h>
struct tree
{
int data;
struct tree *lchild;
struct tree *rchild;
}*t,*temp;
int element;
void inorder(struct tree *);
struct tree * create(struct tree *, int);
struct tree * insert(struct tree *, int);
struct tree * del(struct tree *, int);
struct tree * findmin(struct tree *);
void main()
{
int ch;
clrscr();
cout<<"\n\t\t\tBINARY SEARCH TREE";
do
{
cout<<"\n\t\t\t****** ****** ****";
cout<<"\nMain Menu\n";
cout<<"\n 1.Create\n 2.Insert\n 3.Delete\n 4.Exit";
cout<<"\nEnter ur choice :";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the data:";
cin>>element;
t=create(t,element);
inorder(t);
break;
case 2:
cout<<"\nEnter the data:";
cin>>element;
t=insert(t,element);
inorder(t);
break;
case 3:
cout<<"\nEnter the data:";
cin>>element;
t=del(t,element);
inorder(t);

P a g e | 54
break;
case 4:
exit(0);
}
}while(ch<=4);
}
struct tree * create(struct tree *t, int element)
{
t=(struct tree *)malloc(sizeof(struct tree));
t->data=element;
t->lchild=NULL;
t->rchild=NULL;
return t;
}
struct tree *insert(struct tree *t,int element)
{
if(t==NULL)
{
t=(struct tree *)malloc(sizeof(struct tree));
t->data=element;
t->lchild=NULL;
t->rchild=NULL;
return t;
}
else
{
if(element<t->data)
{
t->lchild=insert(t->lchild,element);
}
else
if(element>t->data)
{
t->rchild=insert(t->rchild,element);
}
else
if(element==t->data)
{
cout<<"element already present\n";
}
return t;
}
}
struct tree * del(struct tree *t, int element)
{
if(t==NULL)

P a g e | 55
cout<<"element not found\n";
else
if(element<t->data)
t->lchild=del(t->lchild,element);
else
if(element>t->data)
t->rchild=del(t->rchild,element);
else
if(t->lchild&&t->rchild)
{
temp=findmin(t->rchild);
t->data=temp->data;
t->rchild=del(t->rchild,t->data);
}
else
{
temp=t;
if(t->lchild==NULL)
t=t->rchild;
else
if(t->rchild==NULL)
t=t->lchild;
free(temp);
}
return t;
}
struct tree *findmin(struct tree *t)
{
if(t==NULL)
return NULL;
else
if(t->lchild==NULL)
return t;
else
return(findmin(t->lchild));
}
void inorder(struct tree *t)
{
if(t==NULL)
return;
else
{
inorder(t->lchild);
cout<<" "<<t->data;
inorder(t->rchild);
}

P a g e | 56
}
OUTPUT:
BINARY SEARCH TREE
Main Menu
1.Create
2.Insert
3.Delete
4.Exit
Enter ur choice :1
Enter the data:22
22
Main Menu
1.Create
2.Insert
3.Delete
4.Exit
Enter ur choice :2
Enter the data:11
11 22
Main Menu
1.Create
2.Insert
3.Delete
4.Exit
Enter ur choice :2
Enter the data:33
11 22
33
Main Menu
1.Create
2.Insert
3.Delete
4.Exit
Enter ur choice :2
Enter the data:43
11 22
33
Main Menu
1.Create
2.Insert
3.Delete

43

P a g e | 57
4.Exit
Enter ur choice :3
Enter the data:33
11 22
43
Main Menu
1.Create
2.Insert
3.Delete
4.Exit
Enter ur choice :4
RESULT:
Thus the Binary search tree has been successfully executed.

9.HEAP SORT
AIM :
To write a C++ program for Heap sort.
ALGORITHM:
1. Start the program
2. Construct a min heap, such that the value of the root node should be less than the left
key value & right key value.
3. Use an empty array for storing the sorted elements
4. Delete the root value (minimum element) and store it as a first element of the array.
5. Delete the last value of the root and store it as a root
6. Reconstruct the min heap
7. Repeat these steps until we get the sorted array and display it.
8. Stop the program.
PROGRAM:
#include <iostream.h>
#include <conio.h>

P a g e | 58
void fnSortHeap(int[], int);
void main()
{
int i, n, arr[20];
clrscr();
cout<<"\nEnter the no. of elements in the array:";
cin>>n;
for(i=0;i<n;i++)
cin>>arr[i];
for(i=n; i>1; i--)
{
fnSortHeap(arr, i - 1);
}
cout<<"\nThe Sorted Array\n----------------\n";
for (i = 0; i < n; i++)
cout<<" "<<arr[i];
getch();
}
void fnSortHeap(int arr[], int arr_ubound)
{
int i,o;
int lChild, rChild, mChild, root, temp;
//find the root element of the current element
root = (arr_ubound-1)/2;
//creating the heap
for(o=root;o>=0;o--)
{
for(i=root;i>=0;i--)
{
lChild = (2*i)+1;
rChild = (2*i)+2;
if ((lChild <= arr_ubound) && (rChild <= arr_ubound))
{
if(arr[rChild] >= arr[lChild])
mChild = rChild;
else
mChild = lChild;
}
else
{
if(rChild > arr_ubound)
mChild = lChild;
else
mChild = rChild;
}
//swap elements

P a g e | 59
if (arr[i] < arr[mChild])
{
temp = arr[i];
arr[i] = arr[mChild];
arr[mChild] = temp;
}
}
}
//move the max element to the end of the array
temp = arr[0];
arr[0] = arr[arr_ubound];
arr[arr_ubound] = temp;
}
OUTPUT:
Enter the no. of elements in the array:10
9 7 5 3 1 2 10 8 6 4
The Sorted Array
---------------1 2 3 4 5 6 7 8 9 10
RESULT:
Thus the C++ program for implementing heap sort has been successfully executed.
10.QUICK SORT
AIM :
To write a C++ program for Quick sort.
ALGORITHM:
1. Start the program.
2. Read the number of elements to be sorted.
3. Initialize a pivot (center), left and right positions.
4. Check whether the center element is less than the left element. Then swap left and
center values.
5. Check whether the right element is less than the left element. Then swap right and left
values.
6. Check whether the right element is less than the center element. Then swap center and
right.
7. Finally the element in the left of pivot will be less than the pivot element and the
element to the right of the pivot will be greater than the pivot element .
8. Repeat the steps until the given elements are arranged in a sorted manner.
9. Stop the program.

P a g e | 60

PROGRAM:
#include<iostream.h>
#include<conio.h>
int partition(int low,int high,int arr[]);
void quick_sort(int low,int high,int arr[]);
void main()
{
int a[20],n,low,high,i;
clrscr();
cout<<"\n Enter number of elements:";
cin>>n;
cout<<"\n Enter the elements:";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\n\n Initial Order of elements:\n";
for(i=0;i<n;i++)
cout<<" "<<a[i];
high=n-1;
low=0;
quick_sort(low,high,a);
cout<<"\n\n Final Array After Sorting:\n";
for(i=0;i<n;i++)
cout<<" "<<a[i];
getch();
} /*Function for partitioning the array*/
int partition(int low,int high,int arr[])
{
int i,high_vac,low_vac,pivot/*,itr*/;
pivot=arr[low];
while(high>low)
{
high_vac=arr[high];
while(pivot<high_vac)
{
if(high<=low) break;
high--;
high_vac=arr[high];
}
arr[low]=high_vac;
low_vac=arr[low];
while(pivot>low_vac)
{
if(high<=low) break;
low++;
low_vac=arr[low];

P a g e | 61
}
arr[high]=low_vac;
}
arr[low]=pivot;
return low;
}
void quick_sort(int low,int high,int arr[])
{
int piv_index,i;
if(low<high)
{
piv_index=partition(low,high,arr);
quick_sort(low,piv_index-1,arr);
quick_sort(piv_index+1,high,arr);
}
}
OUTPUT :
Enter number of elements:10
Enter the elements:5 9 7 3 1 8 6 10 2 4
Initial Order of elements:
5 9 7 3 1 8 6 10 2 4
Final Array After Sorting:
1 2 3 4 5 6 7 8 9 10
RESULT:
Thus the Quick sort has been successfully executed.
11.POLYMORPHISM
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void area(int);
void area(int,int);
void area(float);
void main()
{
int r,l,b,ch;
float x;
clrscr();
do
{
cout<<"\n1.area of the circle\n 2.area of rectangular \n 3.area of square \n 4.exit\n";
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:

P a g e | 62
cout<<"\nEnter radius:";
cin>>r;
area(r);
break;
case 2:
cout<<"\nEnter length and breadth:";
cin>>l>>b;
area(l,b);
break;
case 3:
cout<<"\nEnter side:";
cin>>x;
area(x);
break;
case 4:
exit(0);
}
}while(ch<=4);
getch();
}
void area(int r)
{
float ar;
ar=3.14*r*r;
cout<<"\nThe area of the circle is:"<<ar;
}
void area(int l,int b)
{
int rec_area;
rec_area=l*b;
cout<<"\nThe area of the rectangle is:"<<rec_area;
}
void area(float x)
{
float sq_ar;
sqi_ar=x*x;
cout<<"\nThe area of the square is:"<<tri_ar;
}
OUTPUT
1.area of the circle
2.area of rectangular
3.area of square
4.exit
Enter your choice:1

P a g e | 63

Enter radius:2
The area of the circle is:12.56
1.area of the circle
2.area of rectangular
3.area of square
4.exit
Enter your choice:2
Enter length and breadth:2 4
The area of the rectangle is:8
1.area of the circle
2.area of rectangular
3.area of square
4.exit
Enter your choice:3
Enter side:2
The area of the square is:4
1.area of the circle
2.area of rectangular
3.area of square
4.exit
Enter your choice:4