PROGRAM NO.
FACTORIAL USING RECURSION
#include<iostream.h> #include<conio.h> int fact(int); main() { int n,res; cout<<"\nEnter the no whose factorial is to be found out:"; cin>>n; res=fact(n); cout<<"\n The answer is"; cout<<"\t"<<res; getch(); } int fact(int n) { int f; if(n==1) return 1 else f=n*fact(n-1)
return(f); }
PROGRAM NO STACK IMPLEMENTATION USING ARRAYS
#include<iostream.h> #include<conio.h> #include<process.h> int push(int[],int&,int); int pop(int[],int&); void display(int[],int); void main() { int size,i,c,res,item; int top=-1; int a[50]; cout<<"\n STACK MENU"; cout<<"\n Enter 1 for insertion:"; cout<<"\n Enter 2 for deletion:"; cout<<"\n Enter 3 for display:"; cout<<"\n Enter your choice"; cin>>c; switch(c) { case 1: cout<<"\nEnter the element that you want to insert";
cin>>item; res=push(a,top,item); if(res==-1) { cout<<"\nOVERFLOW"; exit(1); } cout<<"\nStack is now as"; display(a,top); break;
case 2: res=pop(a,top); cout<<"\n Now the deletion begins"; if(res==-1) { cout<<"\nUNDERFLOW"; exit(1);
} else { cout<<"\n The element that has been deleted is"; cout<<res; } cout<<"\nThe stack now is as"; display(a,top); break;
case 3: cout<<"\n The stack is"; display(a,top); break;
case 4: cout<<"\n Enter any valid choice"; break;
getch(); }
int push(int a[],int &top,int ele) { if(top==49) { return -1; } else { top=top+1; a[top]=ele; }
return 0; }
int pop(int a[],int top) { int ret; if(top==-1) { return -1; } else { ret=a[top]; top--; } return ret; }
void display(int a[],int top) { for(int i=top;i>=0;i--) cout<<a[i]<<"\t"; }
PROGRAM NO STACK IMPLEMENTATION USING LINKED LISTS
#include<iostream.h> #include<conio.h> #include<process.h> struct node{ int info; node* next; }*top,*save,*ptr,*newptr; node* createnode(int); void insert(node*); void remove(); void display(node*);
void main() { clrscr(); int inf,c; top=NULL; char ch='y'; while(ch=='y') { cout<<"\Enter the information to be inserted into the node:"; cin>>inf; newptr=createnode(inf);
if (newptr==NULL) { cout<<"\n The element could not be created"; exit(1); } push(newptr);
cout<<"\nDo you want to enter more elements"; cin>>ch; } cout<<"\n STACK MENU"; cout<<"\n Enter 1 for deleting an element out of the stack"; cout<<"\n Enter 2 for display"; cin>>c; switch(c) {
case 1: pop(); display(top); break; case 2: cout<<"\n The elements are as follows"; display(top); break;
default: cout<<"\n The valid choices are 1,2,3"; break;
getch(); } node* createnode(int inf) { ptr=new node; ptr->info=inf; ptr->next=NULL; return ptr; }
void push(node* ptr1) { if (top==NULL) { top=ptr1; } else { save=top; top=ptr1; ptr1->next=save
} }
void pop() { if(top==NULL) { cout<<"\Nunderflow"; } else { ptr=top; top=top->next; delete ptr; } }
void display(node* ptr3) {
while(ptr3!=NULL) { cout<<ptr3->info<<"->"; ptr3=ptr3->next; }
PROGRAM NO QUEUE IMPLENTATION
#include<iostream.h> #include<conio.h> #include<process.h> struct node{ int info; node* next; }*front,*rear,*save,*ptr,*newptr; node* createnode(int); void insert(node*); void remove(); void display(node*);
void main() { clrscr(); int inf,c; front=rear=NULL; char ch='y'; while(ch=='y') { cout<<"\Enter the information to be inserted into the node:"; cin>>inf; newptr=createnode(inf);
if (newptr==NULL) { cout<<"\n The element coud not be created"; exit(1); } insert(newptr);
cout<<"\nDo you want to enter more elemnts"; cin>>ch; } cout<<"\n QUEUE MENU"; cout<<"\n Enter 1 for deleting an element out of the queue"; cout<<"\n Enter 2 for display"; cin>>c; switch(c) {
case 1: remove(); display(front); break; case 2: cout<<"\n The elements are as follows"; display(front); break;
default: cout<<"\n The valid choices are 1,2,3"; break;
getch(); } node* createnode(int inf) { ptr=new node; ptr->info=inf; ptr->next=NULL; return ptr; }
void insert(node* ptr1) { if (front==NULL) { front=rear=ptr1; } else { rear->next=ptr1; rear=ptr1; } }
void remove() {
if(front==NULL) { cout<<"\Nunderflow"; } else { ptr=front; front=front->next; delete ptr; } }
void display(node* ptr3) {
while(ptr3!=NULL) { cout<<ptr3->info<<"->"; ptr3=ptr3->next;
PROGRAM NO CIRCULAR QUEUE IMPLEMENTATION
#include<iostream.h> #include<conio.h> #include<process.h> int insert(int[],int); int del(int[]); void display(int[],int,int); const int size=10; int front=-1,rear=-1; void main() { clrscr(); int queue[size]; int c,res,ele; int item; do{ cout<<"\nCIRCULAR QUEUES"; cout<<"\nEnter 1 for insertion"; cout<<"\nEnter 2 for deletion"; cout<<"\nEnter 3 for display"; cout<<"\nEnter 4 for exit"; cout<<"\Enter your choice"; cin>>c; switch(c)
{ case 1:cout<<"\nEnter the element that you want to insert"; cin>>ele; res=insert(queue,ele); if(res==-1) { cout<<"\nOverflow"; } else { cout<<"\n The circular queue now is"; display(queue,front,rear); } getch(); break;
case 2: item=del(queue); cout<<"\nThe element deleted is "; cout<<item; cout<<"\n The circular queue is";
case 3: display(queue,front,rear); getch(); break;
case 4 :break; default: cout<<"\n The valid choices are only 1,2,3,4";
getch(); break; } }while(c!=4);
} int insert(int a[],int n) {
if(rear==-1) front=rear=0; else if(rear==size-1) rear=0; else rear++; a[rear]=n; return 0; }
void display(int a[],int front,int rear) { int i; if(front==-1) return; if(rear>=front) { for(i=front;i<=rear;i++) { cout<<a[i]; }
} else { for(i=0;i<=rear;i++) { cout<<a[i]; } for(i=front;i<size;i++) { cout<<a[i]; } } } int del(int a[]) { int ret; if(front==-1) { return -1; } else { ret=a[front]; if(front==rear)front=rear=-1; else if(front==size-1)front=0; else front++; }
PROGRAM NO LINEAR SEARCH
#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[30],x,n,pos,i,c; pos=-1; cout<<"\nEnter the size of the array;"; cin>>n; cout<<"\nEnter the elements of the array:"; for(i=0;i<n;i++) { cin>>a[i]; } cout<<"\nEnter the element to be searched"; cin>>x; c=0; while((pos==-1)&&(c<n)) { if(a[c]==x) { pos=c; } c++; }
if(pos==-1) { cout<<"\n The seacrh was unsuccessful"; } else { cout<<"\n The required elemnt is at the position:"<<pos; } getch(); }
PROGRAM NO BINARY SEARCH
#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[30],n,x; cout<<"\nEnter the size of the array:"; cin>>n; cout<<"\nEnter the elements of the array in the ascending order:"; for(int i=0;i<n;i++) { cin>>a[i]; } int first=0,last=n-1; int middle; int pos=-1; cout<<"\nEnter the element to be searched"; cin>>x; while((first<=last)&&(pos==-1)) { middle=(first+last)/2; if(a[middle]==x) { pos=middle;
} else if(a[middle]<x) { first=middle+1; } else { last=middle-1; } } if(pos==-1) { cout<<"\n the search was unsuccessful"; } else { cout<<"\n The value is stored at position"<<++pos; } getch(); }
PROGRAM NO BUBBLE SORTING
#include<iostream.h> #include<conio.h> void bubblesort(int[],int); void main() { int arr[50],n; cout<<"\nEnter the size of the array"; cin>>n; cout<<"\nEnter the elements of the array"; for(int i=0;i<n;i++) { cin>>arr[i]; } bubblesort(arr,n); getch(); } void bubblesort(int a[],int n) { int temp; for(int i=0;i<n;i++) { for(int j=0;j<n-1-i;j++) {
if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } cout<<"\nThe elements of the array in the ascending order are"; for(i=0;i<n;i++) { cout<<a[i]<<"\t"; } }
PROGRAM NO SELECTION SORTING
#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[30],i,pos,temp,n,flag,j; cout<<"\nEnter the size of the array;"; cin>>n; cout<<"\nEnter the elements of the array:"; for(i=0;i<n;i++) { cin>>a[i]; } flag=0; while((i<n-1)&&!flag) { i++; flag=0; for(j=0;j<n;j++) { if(a[j]>a[j+1])
{ temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; flag=0; } }
} cout<<"\nThe sorted list is:"; for(i=0;i<n;i++) { cout<<a[i]; } getch(); }
PROGRAM NO INSERTION SORTING
#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[30],i,pos,temp,n,flag,j; cout<<"\nEnter the size of the array;"; cin>>n; cout<<"\nEnter the elements of the array:"; for(i=0;i<n;i++) { cin>>a[i]; } for(i=1;i<n;i++) { temp=a[i]; j=i-1;
while((temp<a[j])&&j>=0)
{ a[j+1]=a[j]; j=j-1; } a[j+1]=temp; }
cout<<"\nThe sorted list is:"; for(i=0;i<n;i++) { cout<<a[i]; } getch(); }