oops hi
oops hi
oops hi
1
Tower of Hanoi Using Recursion
2
Binary Search Tree Using Traversal
3
Stack Using Linked List
4
Circular Queue
5
Quick Sort
6
Heap Sort
7
Knapsack Problem Using Greedy Method
8 Search Element in a Tree Using Divide And
Conquer
9
Matrix
10
Virtual Function
11
Parameterized Constructor
12
Friend Function
13
Function Overloading
14
Single Inheritance
15
Employee Details Using Files
EX.NO:01
TOWER OF HANOI USING RECURSION
DATE:
AIM:
ALGORITHM:
#include<iostream.h>
#include<conio.h>
Void TowerOfHanoi()
if(num>0) {
TowerOfHanoi(num-1,A,C,B);
<<”To”<<” “<<C<<endl<<endl;
TowerOfHanoi(num-1,B,A,C);
#include<conio.h>
void TowerOfHanoi(int,char,char,char);
void main()
int numOfDisk;
clrscr();
cin>>numOfDisk;
TowerOfHanoi(numOfDisk,’A’,’B’,’C’);
cout<<endl;
getch();
if(num>0)
TowerOfHanoi(num-1,A,C,B);
TowerOfHanoi(num-1,B,A,C);
}
OUTPUT:
RESULT:
Thus the given C++ program has been executed successfully and output was verified.
EX.NO:2
BINARY SEARCH TREE USING TRAVERSAL
DATE:
AIM:
ALGORITHM:
#include<iostream.h>
#include<conio.h>
#include<conio.h>
class Tree
public:
int data;
Tree *left,*right;
Tree(int x)
data=x;
left=NULL;
right=NULL;
};
if(root==NULL)
return;
cout<<root->data<<" ";
preorder_traversal(root->left);
preorder_traversal(root->right);
{
if(root==NULL)
return;
inorder_traversal(root->left);
cout<<root->data<<" ";
inorder_traversal(root->right);
if(root==NULL)
return;
postorder_traversal(root->left);
postorder_traversal(root->right);
cout<<root->data<<" ";
void main()
root->left=new Tree(10);
root->right=new Tree(11);
root->left->left=new Tree(7);
root->right->left=new Tree(27);
root->right->right=new Tree(9);
cout<<"Preorder=";
preorder_traversal(root);
cout<<endl;
cout<<"Inorder= ";
inorder_traversal(root);
cout<<endl;
cout<<"Postorder= ";
postorder_traversal(root);
cout<<endl;
getch();
}
OUTPUT:
RESULT:
Thus the given C++ program has been executed successfully and output was verified.
EX.NO:03
STACK USING LINKED LIST
DATE:
AIM:
ALGORITHM:
#include<iostream.h>
#include<conio.h>
#include<malloc.h>
#include<string.h>
void push () //used for insert the element into the stack
void pop () //used for delete the element into the stack
#include<malloc.h>
#include<conio.h>
#include<string.h>
struct Node
int data;
};
newnode->data= val;
newnode->next= top;
top= newnode;
void pop()
if(top == NULL)
else
void display()
if(top==NULL)
cout<<"Stack Is Empty";
else
ptr= top;
while(ptr!=NULL)
cout<<ptr->data<<" ";
ptr= ptr->next;
cout<<endl;
cout<<endl;
void main()
int ch,val;
clrscr();
cout<<"1.Push in Stack"<<endl;
cout<<"3.Display Stack"<<endl;
cout<<"4.Exit"<<endl;
do
cin>>ch;
switch(ch)
case 1:
cin>>val;
push(val);
break;
case 2:
pop();
break;
case 3:
{
display();
break;
case 4:
cout<<"Exit"<<endl;
break;
default:
cout<<"INVALID CHOICE"<<endl;
while(ch!=4);
getch();
}
OUTPUT:
RESULT:
Thus the given C++ program has been executed successfully and output was verified.
EX.NO:4
CIRCULAR QUEUE
DATE:
AIM:
ALGORITHM:
#include<iostream.h>
#include<conio.h>
Void deleteCQ()
Void displayCQ()
Cin>>ch;
#include<conio.h>
int cqueue[5];
cout<<"~*~*Queue IS OverFlow~*~*\n";
return;
if (front==-1)
front=0;
rear=0;
else
if(rear==n-1)
rear=0;
else
rear=rear+1;
}
cqueue[rear]=val;
void deleteCQ()
if(front==-1)
cout<<"Queue IS Underflow\n";
return;
if(front == rear)
front=-1;
rear=-1;
else
if(front==n-1)
front=0;
else
front=front+1;
void displayCQ()
{
int f=front, r=rear;
if(front==-1)
cout<<"QUEUE IS EMPTY!!!"<<endl;
return;
if(f<=r)
while(f<=r)
cout<<cqueue[f]<<" ";
f++;
else
while(f<=n-1)
cout<<cqueue[f]<<" ";
f++;
f=0;
while(f<=r)
{
cout<<cqueue[f]<<" ";
f++;
cout<<endl;
void main()
int ch,val;
clrscr();
cout<<"(1)-->INQUEUE--\n";
cout<<"(2)-->DEQUEUE--\n";
cout<<"(3)-->DISPLAY--\n";
cout<<"(4)-->EXIT--\n";
do
cout<<"ENTER--YOUR--CHOICE-->:"<<endl;
cin>>ch;
switch(ch)
case 1:
cin>>val;
insertCQ(val);
break;
case 2:
deleteCQ();
break;
case 3:
displayCQ();
break;
case 4:
cout<<"**!!EXIT!!**\n";
break;
default:
cout<<"!!!INCORRECT CHOICE~*~*~\n";
while(ch!=4);
getch();
}
OUTPUT:
RESULT:
Thus the given C++ program has been executed successfully and output was verified.
EX.NO:05
QUICK SORT
DATE:
AIM:
To write a c++ program to perform quick sort.
ALGORITHM:
Quick_sort(a,1,j-1);quick_sort(a,j+1,u)
Step 7: And then using the int partition value and declare the value
#include<conio.h>
if(high>low)
int pp=partition(low,high,pp,s);
quicksort(low,pp-1,s);
quicksort(pp+1,high,s);
cout<<"\n";
for(int i=low;i<high;i++)
cout<<"\tS:"<<s[i];
int i,j,pi,temp,temp1;
pi=s[low];
j=low;
for(i=low;i<=high;i++)
{
if(s[i]<pi)
j++;
temp=s[i];
s[i]=s[j];
s[j]=temp;
pp=j;
temp1=s[low];
s[low]=s[pp];
s[pp]=temp1;
return(pp);
void main()
textcolor(GREEN);
textbackground(BLUE);
clrscr();
int n,i,s[20],low;
low=1;
cin>>n;
cin>>s[i];
quicksort(low,n,s);
getch();
}
OUTPUT:
RESULT:
Thus the given C++ program has been executed successfully and output was verified.
EX.NO:6
HEAP SORT
DATE:
AIM:
ALGORITHM:
#include<iostream.h>
#include<conio.h>
Step 8: Print the Elements for before sorting and after sorting using cout.
#include<conio.h>
int largest=i;
int left=2*i+1;
int right=2*i+2;
if(left<n&&a[left]>a[largest])
largest=left;
if(right<n&&a[right]>a[largest])
largest=right;
if(largest!=i)
int temp=a[i];
a[i]=a[largest];
a[largest]=temp;
heapify(a,n,largest);
for(int i=n/2-1;i>=0;i--)
heapify(a,n,i);
for(i=n-1;i>=0;i--)
{ //swap(a[0],a[i]);
int temp=a[0];
a[0]=a[i];
a[i]=temp;
heapify(a,i,0);
for(int i=0;i<n;++i)
cout<<a[i]<<" ";
void main()
int a[]={47,9,22,42,27,25,0};
int n=sizeof(a)/sizeof(a[0]);
clrscr();
printArr(a,n);
heapsort(a,n);
printArr(a,n);
getch();
}
OUTPUT:
RESULT:
Thus the given C++ program has been executed successfully and output was verified.
EX.NO:7
KNAPSACK PROBLEM
DATE:
AIM:
ALGORITHM:
#include<iostream.h>
#include<conio.h>
If(n==0||w==0)
Return 0;
Cout<<”\n knapsack(W,weight,profit,n)
#include<conio.h>
return(a>b)?a:b;
if(n==0||W==0)
return 0;
if(wt[n-1]>W)
return knapSack(W,wt,val,n-1);
else
return max(
val[n-1]+knapSack(W-wt[n-1],wt,val,n-1),knapSack(W,wt,val,n-1));
void main(){
int profit[]={60,100,120};
int weight[]={10,20,30};
int W=50;
clrscr();
cout<<"--->"<<knapSack(W,weight,profit,n);
getch();
}
OUTPUT:
RESULT:
Thus the given C++ program has been executed successfully and output was verified.
-EX.NO:08
DIVIDE AND CONQUER
DATE:
AIM:
To write a program for Searching element in tree using divide and conquer
in C++.
ALGORITHM:
#include<iostream.h>
#include<conio.h>
Void binary_search ()
#include<conio.h>
int low=0;
int high=len-1;
while(low<=high)
int mid=(low+(high-low)/2);
if(a[mid]==key)
return mid;
if(key<a[mid])
high=mid-1;
else
low=mid+1;
return -1;
}
void main()
int a[10]={1,3,5,7,9,11,13,15,17,21};
int key = 5;
int position=binary_search(a,key,10);
clrscr();
cout<<"\n\n";
if(position==-1)
cout<<"\tNOT FOUND!!!"<<endl;
else
getch();
}
OUTPUT:
RESULT:
Thus the given C++ program has been executed successfully and output was verified.
-EX.NO:09
MATRIX
DATE:
AIM:
ALGORITHM:
#include<iostream.h>
#include<conio.h>
Step 5:check the queen in the matrix using bool isvalid`() function.
Step6: search the queen in the matrix using bool bool search()
Function.
int queens[NUMBER_OF_QUEENS];
return true;
return false;
void main()
int inputRow;
search(inputRow); to 7
printResult(inputRow);
}
OUTPUT:
RESULT:
Thus the given C++ program has been executed successfully and output was verified.
EX.NO:10
VIRTUAL FUNCTION
DATE:
AIM:
ALGORITHM:
#include<iostream.h>
#include<conio.h>
#include<conio.h>
class A{
int x;
public:
void display(){
x=5;
cout<<"Value of X Is--->:"<<x<<endl;
};
class B:public A{
int y;
public:
void display(){
y=5;
cout<<"Value Of Y Is--->:"<<y<<endl;
};
void main(){
A*a;
B b;
a=&b;
a->display();
getch();}
OUTPUT:
RESULT:
Thus the given C++ program has been executed successfully and output was verified.
EX.NO:11
PARMETERIZED CONSTRUCTOR
DATE:
AIM:
ALGORITHM:
#include<iostream.h>
#include<conio.h>
Void add ()
C=a+b;
Cout<<”total:”<<c<<endl;
#include<conio.h>
class math
Private:
int a,b,c;
public:
math(int x,int y)
a=x;
b=y;
void add()
c=a+b;
cout<<”Total”<<c;
};
void main()
math o(10,25);
clrscr();
o.add();
}
OUTPUT:
RESULT:
Thus the given C++ program has been executed successfully and output was verified.
EX.NO:12
FRIEND FUNCTION
DATE:
AIM:
ALGORITHM:
#include<iostream.h>
#include<conio.h>
Void get()
Void show()
If(strcmp(s.str,a.str)==0)
else
#include<conio.h>
#include<string.h>
class string
char str[20];
public:
void get()
cin>>str;
void show()
int c;
if(strcmp(s.str,a.str)==0)
else
}
};
void main()
string a,b;
clrscr();
cout<<"\t\t\t*******FRIEND FUNCTION*******"<<endl;
a.get();
a.show();
b.get();
b.show();
a==b;
getch();
}
OUTPUT:
RESULT:
Thus the given C++ program has been executed successfully and output was verified.
EX.NO:13
FUNCTION OVERLOADING
DATE:
AIM:
ALGORITHM:
#include <iostream.h>
#include<conio.h>
#include<conio.h>
class measure {
public:
};
void measure::shape(int r)
}
void measure::shape(long a)
void measure::shape(double j)
void main() {
int r,d,e,l,b;
double t,c,h;
long a;
int ch;
double j,f;
long int g;
clrscr();
measure obj;
cout<<"\t CALCULATION OF AREAS & VOLUME";
cout<<"\n\n1.Area of Circle:";
cout<<"\n\n2.Area of Rectangle:";
cout<<"\n\n3.Area of Triangle:";
cout<<"\n\n4.Area of Square:";
cout<<"\n\n5.volume of Cone:";
cout<<"\n\n6.volume of Sphere:";
cout<<"\n\n7.volume of Cylinder:";
cin>>ch;
switch (ch){
case 1:
cin>>r;
obj.shape(r);
break;
case 2:
cin>>l>>b;
obj.shape(l,b);
break;
case 3:
cin>>d>>e;
obj.shape(0.5,d,e);
break;
case 4:
cin>>a;
obj.shape(a);
break;
case 5:
cin>>c;
cin>>g;
obj.shape(c,g);
break;
case 6:
cin>>j;
obj.shape(j);
break;
case 7:
cin>>f;
cin>>h;
obj.shape(h,f);
break;
default:
getch();
}
OUTPUT:
RESULT:
Thus the given C++ program has been executed successfully and output was verified.
EX.NO:14
SINGLE INHERITANCE
DATE:
AIM:
ALGORITHM:
#include<iostream.h>
#include<conio.h>
Step 3: Declare the variable name, roll no, tam, eng, mat, tot, and avg.
s.getdata ();
s.display ();
#include<conio.h>
class st
protected:
int tam,eng,mat,tol,avg,rollno;
char name[10];
public:
void getdata()
cin>>name;
cin>>rollno;
cin>>tam;
cin>>eng;
cin>>mat;
tol=tam+eng+mat;
avg=tol/3;
}
};
class n:public st
public:
void display()
cout<<"***~~STUDENTS DETAILS~~~***"<<endl;
cout<<"NAME:->"<<name<<endl;
cout<<"ROLL NO:->"<<rollno<<endl;
cout<<"TAMIL:->"<<tam<<endl;
cout<<"ENGLISH:->"<<eng<<endl;
cout<<"MATHS:->"<<mat<<endl;
cout<<"TOTAL:->>"<<tol<<endl;
cout<<"AVERAGE:->>"<<avg<<endl;
};
void main(){
n s;
clrscr();
s.getdata();
s.display();
getch();
}
OUTPUT:
RESULT:
Thus the given C++ program has been executed successfully and output was verified.
EX.NO:15
EMPLOYEE DETAILS USING FILE
DATE:
AIM:
ALGORITHM:
#include<fstream.h>
#include<iostream.h>
Outfile<<data<<endl;
Cout<<data<<endl;
Outfile.close();
Infile.close();
#include<fstream.h>
#include<conio.h>
void main() {
char data[100];
clrscr();
ofstream outfile;
outfile.open("afile.dat");
cin.getline(data,100);
outfile<<data<<endl;
cin>>data;
cin.ignore();
outfile<<data<<endl;
outfile.close();
ifstream infile;
infile.open("afile.dat");
infile>>data;
cout<<data<<endl;
infile>>data;
cout<<data<<endl;
infile.close();
getch();
}
OUTPUT:
RESULT:
Thus the given C++ program has been executed successfully and output was verified.