Program No. Factorial Using Recursion
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); }
#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;
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; }
#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);
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;
getch(); } node* createnode(int inf) { ptr=new node; ptr->info=inf; ptr->next=NULL; return ptr; }
} }
#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);
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;
getch(); } node* createnode(int inf) { ptr=new node; ptr->info=inf; ptr->next=NULL; return ptr; }
void remove() {
#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 4 :break; default: cout<<"\n The valid choices are only 1,2,3,4";
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++; }
if(pos==-1) { cout<<"\n The seacrh was unsuccessful"; } else { cout<<"\n The required elemnt is at the position:"<<pos; } getch(); }
#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(); }
#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"; } }
#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])
#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)