You are on page 1of 17

bUBBLE SORTING

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int temp,list[100],n,i,j;
cout<<"enter the number\n\a";
cin>>n;
cout<<"enter the elements in a list\n";
for(i=0;i<n;i++)
cin>>list[i];
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(list[j]>list[j+1])
{
temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
cout<<list[i]<<" ";
getch();
}

2) CIRCULAR QUEUE
#include<iostream.h>
#include<conio.h>
#include<process.h>
class queue
{
int data[10];
int front,rear;
public:
queue()
{front=-1;
rear=-1;
}
void add();
void remove();
void display();
};
void queue::add()
{
if((rear+1==front)||(rear==9&&front==0))
{
cout<<"Overflow ";
}
else
{
if((rear==-1) &&(front==-1))
{
rear=0;
front=0;
}
else if(rear==9)
{
rear=0;
}
else
{
rear++;
}
cout<<"Enter the element ";
cin>>data[rear];
}
}
void queue::remove()
{
if(front==-1&&rear==-1)
{
cout<<"Underflow ";
}
else
{
if(front==9)
{
front=0;
}
else if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front++;
}
}
}
void queue::display()
{
int i=0,n=9;
If(rear==-1)
{
cout<<"No elements.."<<endl;
}
else
{
if(rear>front)
{
for(i=0;i<front;i++)
{
cout<<"_";
}
for(i=front;i<=rear;i++)
{
cout<<data[i];
}
for(i=rear+1;i<n;i++)
{
cout<<"_";
}
}
else
{
for(i=0;i<=rear;i++)
{
cout<<data[i];
}
for(i=rear+1;i<front;i++)
{
cout<<"_";
}
for(i=front;i<n;i++)
{
cout<<data[i];
}
}
}
}
void main()
{
clrscr();
int ch;
queue queue;
X:
cout<<"\nEnter your choice\n1.Insert\n2.Delete\n3.Display\n4.Exit\n";
cin>>ch;
switch(ch)
{
case 1:queue.add();
goto X;
case 2:queue.remove();
goto X;
case 3:queue.display();
goto X;
case 4:exit(0);
}
getch();
}
3) SUM OF DIAGONALS OF A MATRIX
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3],m,n,i,j,sumd1=0,sumd2=0;
cout<<"enter the number of rows\n";
cin>>m;
cout<<"enter the number of columns\n";
cin>>n;
cout<<"enter the elements of matrix\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
sumd1=sumd1+a[i][j];
}
}
cout<<"the sum of 1st diagonal is-: "<<sumd1<<"\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i+j==m-1)
sumd2=sumd2+a[i][j];
}
}
cout<<"the sum of 2nd diagonal is-: "<<sumd2<<"\n";
getch();
}
4) FIBONACCI SERIES
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
long int i,x=0,y=1,s;
cout<<x<<endl<<y<<endl;
for(i=3;i<=10;i++)
{
s=x+y;
cout<<s<<endl;
x=y;
y=s;
}
getch();
}

5) INSERTION OR DELETION OF ELEMENT IN A QUEUE


#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int ch,i,rear=-1,front=-1,queue[10];
x:
cout<<endl<<endl;
cout<<"Enter Choice 1> Insert 2> Delete 3>exit "<<endl;
cin>>ch;
switch(ch)
{
case 1:
if(front==-1)
{front=0;}
rear++;
if(rear<=9)
{
cout<<"Enter The Element"<<endl;
cin>>queue[rear];
cout<<"Queue is"<<endl;
for(i=0;i<=rear;i++)
{
cout<<queue[i];}
}
else
{
cout<<"****************QUEUE OVERFLOW****************";
}
goto x;
case 2:
if(rear==-1)
{rear=0;}
if(front==-1)
cout<<"*************** UNDER FLOW **********"<<endl;goto x;}
if(rear==front)
{
queue[front]='\o';
front=-1;
rear=-1;
goto x;
}
else
{
queue[front]='\o';
front++;
}
cout<<"Queue is"<<endl;
for(i=0;i<=rear;i++)
{
cout<<queue[i];
}
goto x;
case 3:
exit(0);
default :
goto x;
}
}

6) INSERTION SORTING
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int list[100],n,i,j,pos,small;
cout<<"enter the number\n";
cin>>n;
cout<<"enter the list\n";
for(i=0;i<n;i++)
{
cin>>list[i];
}
//start sorting
for(i=1;i<n;i++)
{
small=list[i];
j=i-1;
while(small<list[j]&&j>=0)
{
list[j+1]=list[j];
j=j-1;
}
list[j+1]=small;
}
for(i=0;i<n;i++)
cout<<list[i]<<" ";
getch();
}

7) LINEAR AND BINARY SEARCH


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int ch,l,u,mid,c=0,i,n,e,a[50];
cout<<"Enter the limit"<<endl;
cin>>n;
cout<<endl<<"Enter the elemnts"<<endl;
for(i=0;i<n;i++)
{
cout<<endl;
cin>>a[i];
}
cout<<"Enter the element to be searched for"<<endl;
cin>>e;
cout<<"Enter Your Choice 1:Linear 2:Binary Search";
cin>>ch;
switch(ch)
{
case 1 :
for(i=0;i<n;i++)
{
if(a[i]==e)
cout<<endl<<"Element is at "<<i+1<<" Position";
c=c+1;
}
if (c==0)
cout<<"Element Not Present";
break;
case 2:
c=0;
l=0;
u=n-1;
while(l<u)
{
mid=(l+u)/2;
if (a[mid]==e)
{
cout<<"Element is at "<<mid+1<<" Position "<<endl;
c++;
break;
}
if(e<a[mid])
u=mid-1;
if (e>a[mid])
l=mid+1;
}
if (c==0)
cout<<"Element not Present"<<endl;
break;
default :
cout<<"Wrong Choice"<<endl;
break;
}
getch();
}

8) MATRIX MULTIPLICATION
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10],b[10][10],c[10][10],i,j,m,n;
cout<<"enter the no. of rows\n";
cin>>m;
cout<<"enter no. of columns\n";
cin>>n;
cout<<"enter the elements of matrix A\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
cout<<"enter the elements of matrix B\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>b[i][j];
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=(a[i][j]*b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<c[i][j]<<" ";
cout<<"\n";
}
}
getch();
}

9) PROGRAM TO CHECK WHETHER STRING IS PALINDROME OR NOT


#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char string[30];
int first,last,found=0;
cout<<"enter the string\n";
cin>>string;
first=0;
last=strlen(string)-1;
while(first<=last && found==0)
{
if(string[first]==string[last])
found=0;
el se
found=1;
first++;
last--;
}
if(found==0)
cout<<"palindrome";
else
cout<<"not a palindrome";
getch();
}

10) QUEUE AS LINKED LIST


#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
#include<string.h>
struct node
{
char name[20];
int age;
node *link;
} *ptr=NULL,*save=NULL;
class queue
{
node *rear,*front;
public:
queue()
{
rear=NULL;
front=NULL;
}
void queins();
void quedel();
void display();
} q;
void queue::queins()
{
ptr=new node;
if(ptr==NULL)
{
cout<<"Queue overflow ";
}
else
{
cout<<"Enter the name ";
gets(ptr->name);
cout<<"Enter the age ";
cin>>ptr->age;
ptr->link=NULL;
if(rear==NULL)
{
rear=ptr;
front=ptr;
}
else
{
rear->link=ptr;
rear=ptr;
}
}
}
void queue::quedel()
{
if(rear==NULL)
{
cout<<"Queue underflow ";
}
else
{
if(front==rear)
{
save=front;
front=NULL;
rear=NULL;
cout<<"Name ";
puts(save->name);
cout<<"Age "<<save->age;
delete save;
}
else
{
save=front;
front=front->link;
cout<<"Name ";
puts(save->name);
cout<<"Age "<<save->age;
delete save;
}
}
void queue::display()
{
if(rear==NULL)
{
cout<<"No elements ";
}
Else
{
ptr=front;
while(ptr!=NULL)
{
cout<<"Name ";
puts(ptr->name);
cout<<"Age "<<ptr->age;
ptr=ptr->link;
}
}
}
void main()
{
clrscr();
int ch;
X:
cout<<"\nEnter your choice\n1.Insert\n2.Delete\n3.Display\n4.Exit\n";
cin>>ch;
switch(ch)
{
case 1:
q.queins();
goto X;
case 2:
q.quedel();
goto X;
case 3:
q.display();
goto X;
case 4:exit(0);
default:
cout<<"Wrong choice ";
goto X;
}
getch();
}

11) REVERSE OF STRING


#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char name[10] ;
int i,length;
cout<<"enter the string\n";
cin>>name;
length=strlen(name);
for(i=length;i>=1;i--)
{
cout<<name[i];
}
getch();
}
12) SELECTION SORTING
# include<iostream.h>
# include<conio.h>
void sort(int list[100],int n);
void main()
{
clrscr();
int list1[100],i,n1;
cout<<"enter'N'\n\a";
cin>>n1;
cout<<"enter the elements in a list\n\a";
for(i=0;i<n1;i++)
cin>>list1[i];
sort(list1,n1);
for(i=0;i<n1;i++)
cout<<list1[i]<<" ";
getch();
}
void sort(int list[100],int n)
{
int j,i,temp,pos,small;
for(i=0;i<n;i++)
{
small=list[i];
pos=i;
for(j=i+1;j<n;j++)
{
if (small>list[j])
{
small=list[j];
pos=j;
}
}
temp=list[pos];
list[pos]=list[i];
list[i]=temp;
}
}
13) USING A STACK AS ARRAY
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int ch,i,top=-1,stack[5];
x:
cout<<endl<<endl;
cout<<"Enter Choice 1> Insert 2> Delete 3>exit "<<endl;
cin>>ch;
switch(ch)
{
case 1:
top++;
if(top<=4)
{
cout<<"Enter The Element"<<endl;
cin>>stack[top];
cout<<"The Stack is"<<endl;
for(i=0;i<=top;i++)
cout<<stack[i];
goto x;
}
else
{
cout<<" ************* Stack OVERFLOW ********** "<<endl;
goto x;
}
case 2:
if(top>=0)
{
top--;
cout<<"Stack is"<<endl;
for(i=0;i<=top;i++)
cout<<stack[i];
goto x;
}
else
{
cout<<"************** Stack UNDER FLOW ***********"<<endl;
goto x;
}
case 3:
exit(0);
default :
cout<<"WRONG CHOICE !!!!!!!!!!! "<<endl;
goto x;
}
}
14) IMPLEMENTATION OF STACK AS LINKED LIST
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
struct node
{
char name[20];
int age;
node *link;
} *ptr=NULL,*save=NULL;
class stack
{
node *top;
public:
stack()
{
top=NULL;
}
void stackpush();
void stackpop();
void display();
}st;
void stack::stackpush()
{
ptr=new node;
if(ptr==NULL)
{
cout<<"Overflow ";
}
else
{
cout<<"Enter the name ";
gets(ptr->name);
cout<<"Enter the age ";
cin>>ptr->age;
ptr->link=NULL;
if(top==NULL)
{
top=ptr;
}
else
{
ptr->link=top;
top=ptr;
}
}
}
void stack::stackpop()
{
if(top==NULL)
{
cout<<"Underflow ";
}
else
{
save=top;
top=top->link;
cout<<"Name ";
puts(save->name);
cout<<"Age "<<save->age;
delete save;
}
}
void stack::display()
{
if(top==NULL)
{
cout<<"No elements.."<<endl;
}
else
{
ptr=top;
while(ptr!=NULL)
{
cout<<"\nName ";
puts(ptr->name);
cout<<"Age "<<ptr->age;
ptr=ptr->link;
}
}
}
void main()
{
clrscr();
int ch;
X:
cout<<"\nEnter your choice\n1.Insert\n2.Delete\n3.Display\n4.Exit\n";
cin>>ch;
switch(ch)
{
case 1:
st.stackpush();
goto X;
case 2:
st.stackpop();
goto X;
case 3:
st.display();
goto X;
default:
cout<<"Wrong choice ";
goto X;
case 4:exit(0);
}
getch();
}
STORING DATA INTO A TEXT FILE BASING UPON LOWER AND UPPERCASE ALPHABETS
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<stdio.h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>
#include<iomanip.h>
#include<ctype.h>
void main()
{
clrscr();
int i,l;
char st[50];
ofstream t1("lower.txt");
ofstream t2("upper.txt");
if(t1&&t2)
{
cout<<"ENTER "<<endl;
gets(st);
l=strlen(st);
for(i=0;i<l;i++)
{
if(islower(st[i]))
t1<<st[i];
if(isupper(st[i]))
t2<<st[i];
}
}
getch();
}

You might also like