WAP To Using Recursion

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 46

1.

/*WAP to using Recursion*/

#include<iostream.h>

#include<conio.h>

void main()

int n,x,fact;

int rec(int);

clrscr();

cout<<"enter the number:-> ";

cin>>n;

fact=rec(n);

cout<<"Factorial result are:"<<fact<<endl;

getch();

rec(int x)

int f;

if(x==1)

return(x);

else

f=x*rec(x-1);

return(f);

}
OUTPUT:
2./*WAP of Traversing element of an Array*/

#include<stdio.h>

#include<conio.h>

void main()

int n,i,k,lb,ub,a[10];

clrscr();

printf("\n enter the total number of element of an array:");

scanf("%d",&n);

printf("\n enter the element in the array: ");

for(i=1;i<=n;i++)

printf("\n element number is:");

scanf("%d",&a[i]);

lb=1;

ub=n;

k=lb;

printf("\n\n");

printf("Traversing of array is:\n");

while(k<=ub)

printf("element number [%d] is %d \n",k,a[k]);

k=k+1;

}
getch();

OUTPUT:
3./*WAP to insert element into an Array*/
#include<stdio.h>

#include<conio.h>

#include<process.h>

int arr[10],n,i,j,k,item,loc;

void main()

clrscr();

printf("Enter the size of array:");

scanf("%d",&n);

if(n>10)

printf("Entered value exceed the limit of array");

getch();

printf("\n Enter array element...");

for(i=0;i<=n;i++)

scanf("%d",&arr[i]);

printf("Enter the element to be inserted...");

scanf("%d",&item);

printf("Enter the location.....");

scanf("%d",&loc);

for(j=n-1;j>=loc;j--)

arr[j+1]=arr[j];

arr[loc]=item;

printf("\n Array after insertion....");

k=0;
while(k<=n)

printf("\n");

printf("%d",arr[k]);

k=k+1;

getch();

OUTPUT:
4./*WAP to Deleting an element from an array*/
#include<stdio.h>

#include<conio.h>

int arr[10],n,i,j,k,item,loc;

void main()

clrscr();

printf("Enter the size of array: ");

scanf("%d",&n);

if(n>10)

printf("Entered the value exceed the limit of array");

getch();

printf("\n Enter array element....");

for(i=0;i<=n-1;i++)

scanf("%d",&arr[i]);

printf("Enter the location of the element to the deleted....");

scanf("%d",&loc);

item=arr[loc];

for(j=loc;j<=n-1;j++)

arr[j]=arr[j+1];

printf("\n Array after deleting....");

k=0;

while(k<=n-2)

printf("\n");
printf("%d",arr[k]);

k=k+1;

getch();

OUTPUT:
5. /* WAP of bubble sort */
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int array[5];

cout<<"Enter five number randomly"<<endl;

for(int i=0;i<5;i++)

cin>>array[i];

cout<<endl;

cout<<"inpu array is:"<<endl;

for(int j=0;j<5;j++)

cout<<"\t\t\t value at"<<j<<"index:"<<array[j]<<endl;

cout<<endl;

int temp;

for(int i2=0;i2<4;i2++)

for(int j=0;j<4;j++)

if(array[j]>array[j+1])

temp=array[j];
array[j]=array[j+1];

array[j+1]=temp;

cout<<"Sorted array is:"<<endl;

for(int i3=0;i3<5;i3++)

cout<<"\t\t\t value at"<<i3<<"index:"<<array[i3]<<endl;

getch();

OUTPUT:
6. /*WAP of Selection Sort*/
#include<stdio.h>

#include<conio.h>

int m;

int minimum(int*,int);

void select_sort(int*);

int minimum(int a[],int i)

int mini,loc;

mini=a[i];

loc=i;

while(i<m)

if(mini>a[i+1])

mini=a[i+1];

loc=i+1;

i++;

return(loc);

void select_sort(int a[])

int temp,i=1,loc;

for(i=0;i<m;i++)

{
loc=minimum [a,i];

temp=a[i];

a[i]=a[loc];

a[loc]=temp;

void display(int a[])

int i;

for(i=0;i<m;i++)

printf("\n\t\t list[%d]=%d",i,a[i]);

void main()

int a[20];

int i;

clrscr();

printf("\n How many element you want to insert:");

scanf("%d",&m);

printf("\n Enter the elements:");

for(i=0;i<m;i++)

printf("\n a[%d]=",i);

scanf("%d",&a[i]);

printf("\n\n\t--------");

printf("\n\t list before sorting:");


printf("\n\t----------");

display(a);

select_sort(a);

printf("\n\n\t-------");

printf("\n\t list after sorting:");

printf("\n\t-------");

display(a);

getch();

}
OUTPUT:

How many element you want to insert: 7

Enter the elements :

43

21

16

30

50

List before sorting:

43

21

16

30

50

List after sorting:

16

21

30

43

50
7. /*WAP of INSERTION SORT*/
#include<stdio.h>

#include<conio.h>

int m;

void insertion_sort(int*);

void display(int*);

void insertion_sort(int a[])

int ele,i;

for(i=1;i<m;i++)

ele=a[i];

while(ele<a[i-1]&&i>0)

a[i]=a[i-1];

i--;

a[i]=ele;

void display(int a[])

int i;

for(i=0;i<m;i++)

printf("\n\n\t list[%d]=%d",i,a[i]);

void main()
{

int a[20];

int i;

clrscr();

printf("\n\n How many element you want to insert:");

scanf("%d",&m);

printf("\n\n Enter the elements:");

for(i=0;i<m;i++)

printf("\n a[%d]=",i);

scanf("%d",&a[i]);

printf("\n\n\n\t--------");

printf("\n\t list before sorting:");

printf("\n\t----------");

display(a);

insertion_sort(a);

printf("\n\n\n\t-------");

printf("\n\t list after sorting:");

printf("\n\t-------");

display(a);

getch();

}
OUTPUT:
8. /* WAP to find the location of an element in the
array using linear search */
#include<stdio.h>

#include<conio.h>

#include<process.h>

int arr[10],n,i,j,k,item,loc;

int flag=0;

void main()

clrscr();

printf("Enter the size of array:");

scanf("%d",&n);

if(n>10)

printf("Entered value exceed the limit of array");

getch();

exit(0);

printf("\n Enter array element------");

for(i=0;i<n-1;i++)

scanf("%d",&arr[i]);

printf("Enter the element to be searched--------");

scanf("%d",&item);

for(j=0;j<=n-1;j++)

if(arr[j]==item)

{
flag=1;

loc=j;

break;

if(flag==1)

printf("\n Element found at location %d",loc);

else

printf("\n Element not found-------");

getch();

OUTPUT:
9./*WAP to find the location of an element in the array
using Binary Search*/
#include<stdio.h>

#include<conio.h>

#include<process.h>

#define true 1

#define false 0

int arr[10],n,i,item,loc,flag=1,low=0,high,middle;

void main()

clrscr();

printf("Enter the size of array:");

scanf("%d",&n);

if(n>10)

printf("Entered value exceed the limit of array");

getch();

exit();

printf("\n Enter array elements-------");

for(i=0;i<n;i++)

scanf("%d",&arr[i]);

printf("Enter the element to be searched----------");

scanf("%d",&item);

low=0;

high=n-1;

while(low<=high)
{

middle=(low+high)/2;

item<arr[middle];

high=middle-1;

if(item==arr[middle])

low=middle+1;

else

flag=1;

loc=middle;

break;

if(flag==1)

printf("\n Element found at location %d",loc);

else

printf("\n Element not found--------");

getch();

}
OUTPUT:

Enter the size of array----------7

Enter array element-----------15

72

32

12

53

22

63

Enter the elewment to be Searched--------------53

Element found at location 4


10./*WAP of Push operation of Stack*/
#include<stdio.h>

#include<conio.h>

#define max 100

int top=-1;

int flag=0;

int stack[max];

void push(int*,int);

void display(int*);

void push(int stack[],int item)

if(top==(max-1))

flag=0;

else

flag=1;

top++;

stack[top]=item;

void display(int stack[])

int i;

if(top==-1)

printf("\n\t\t stack is empty");

}
else

for(i=top;i>=0;i--)

printf("\n\t\t stack[%d]=%d|",i,stack[i]);

void main()

int item;

int n,i;

int top=-1;

clrscr();

printf("How many item you want to push:");

scanf("%d",&n);

for(i=1;i<=n;i++)

printf("\n\n Enter item:");

scanf("%d",&item);

push(stack,item);

if(flag)

printf("\n\n\t\t----------");

printf("\n\t\t stack after push operation:\n");

printf("\n\t\t--------");

display(stack);

else
{

printf("\n\n\t\t-=====");

printf("\n\t\t stack is full");

printf("\n\t\t=====");

getch();

OUTPUT:

How many item you want to push: 4

Enter item: 2

The pushed item is: 23

Stack after push operation:

23
11./*WAP of pop operation*/
#include<stdio.h>

#include<conio.h>

#define max 100

int top=-1;

int flag=0;

int stack[max];

void push(int*,int);

int pop(int*);

void display(int*);

void push(int stack[],int item)

if(top==(max-1))

flag=0;

else

flag=1;

top++;

stack[top]=item;

int pop(int stack[]);

int item;

if(top<0)
{

item=0;

flag=0;

else

flag=1;

item=stack[top];

top--;

return(item);

void display(int stack[])

int i;

if(top==-1)

printf("\n\n\t\t stack is empty");

else

for(i=0;i>=0;i--)

printf("\n\n\t\t|stack[%d]=%d",i,stack[i]);

void main()

{
int n,i,item,f=1;

int top=-1;

clrscr();

printf("\n\n\t\t=====");

printf("\n\t\t stack is empty");

printf("\n\t\t===");

printf("\n\n push some items onto stack");

printf("\n\n how many items you want to push:");

scanf("%d",&n);

for(i=1;i<=n;i++)

printf("\n\n enter item:");

scanf("%d",&item);

push(stack item);

if(flag)

printf("\n\n\t\t--------");

printf("\n\t\t stack after push operation:\n");

printf("\n\t\t-----------");

disaplay(stack);

else

printf("\n\n\t\t===");

printf("\n\t\t stack is full");

printf("\n\t\t===");

}
}

while(f==1)

item=pop(stack);

if(flag)

printf("\n\n the poped item is:",item);

printf("\n\n\n\t\t-----------");

printf("\n\n\t\t stack after pop operation:\n");

printf("\n\t\t-------------");

display(stack);

else

printf("\n\n\n\t\t===");

printf("\n\t\t stack is empty");

printf("\n\t===");

break;

printf("\n\n pop again(1/0):");

scanf("%d",&f);

getch();

}
OUTPUT:

Enter item:4

---------------

Stack after push operation:

---------------

Stack[0]=4

Enter item:9

-------------

Stack after push operation:

------------

Stack[1]=9

Stack[0]=4

Enter item:5

-----------

Stack after push operation:

-----------

Stack[2]=5

Stack[1]=9

Stak[0]=4

The poped item is:5

-----------

Stack after pop operation:

------------

Stack[1]=9

Stack[0]=4
12./*WAP Insert an element in a Circular Queue*/

#include<stdio.h>

#include<conio.h>

#include<process.h>

int que[10],i,max,n,item,front=-1,rear=-1;

void main()

clrscr();

printf("Enter size of circular queue:");

scanf("%d",&n);

if(n>10)

printf("Entered value exceed the limit of queue----");

getch();

exit(0);

printf("\n Enter element of queue---");

for(i=0;i<=n-1;i++)

scanf("%d",&que[i]);

front=0;

rear=rear+1;

printf("Circular queue is-----");


for(i=0;i<=n-1;i++)

printf("%d",que[i]);

max=10;

printf("\n\n Enter the items to be inserted:");

scanf("%d",&item);

if((front==0&&rear==max-1)||front==rear+1)

printf("queue is full overflow!!!");

getch();

exit(0);

if(front==-1)

front=-1;

rear=0;

else

if(rear==max-1)

rear=0;

else

rear=rear+1;

n=n+1;

que[rear]=item;

printf("\n\n item is inserted at rear end-");

printf("\n\n Circular queue AFTER insertion is---------");


for(i=0;i<=n-1;i++)

printf("\t%d",que[i]);

getch();

OUTPUT:
13. /*Merging of two array*/

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int arr1[50],arr2[50],size1,size2,size,i,j,k,merge[100];

cout<<"Enter Array 1 Size:";

cin>>size1;

cout<<"Enter Array 1 Elements:";

for(i=0;i<size1;i++)

cin>>arr1[i];

cout<<"Enter Array 2 Size:";

cin>>size2;

cout<<"Enter Array 2 Elements:";

for(i=0;i<size2;i++)

cin>>arr2[i];

for(i=0;i<size1;i++)

{
merge[i]=arr1[i];

size=size1+size2;

for(i=0,k=size1;k<size&&i<size2;i++,k++)

merge[k]=arr2[i];

cout<<"Now the new array after merging is:\n";

for(i=0;i<size;i++)

cout<<merge[i]<<"";

getch();

OUTPUT:-
14. /*WAP of Postfix*/

#include<stdio.h>

#include<conio.h>

char stack[20];

int top=-1;

void push(char x)

stack[++top]=x;

char pop()

if(top==-1)

return-1;

else

return stack[top--];

int priority(char x)

if(x=='(')

return 0;

if(x=='+'||x=='-')

return 1;

if(x=='*'||x=='/')
return 2;

main ()

char exp[20];

char *e,x;

printf("\n enter the expression::");

scanf("%s",exp);

e=exp;

while(*e!='\0')

if(isalnum(*e))

printf("%c",*e);

else if(*e=='(')

push(*e);

else if(*e==')')

while((x=pop())!='(')

printf("%c",x);

else

while(priority(stack[top])>=priority(*e))

printf("%c",pop());

push(*e);

e++;
}

while(top!=-1)

printf("%c",pop());

getch();

OUTPUT:

enter the expression::A+B

AB+
15. /* Insert a node into a Single Linked List at the
beginning */
# include <stdio.h>

# include <malloc.h>

# include <conio.h>

struct link

int data;

struct link *next;

};

int i;

int number;

struct link start, *previous, *new1;

void insertion(struct link *);

void create_list(struct link *);

void display(struct link *)

void create_list(struct link *node)

char ch;

start.next = NULL;

node = &start; /* Point to the start of the list */

i = 0;

fflush(stdin);

printf("\n Input choice n for break: ");

ch = getchar();

while(ch != 'n')

{
node->next = (struct link* ) malloc(sizeof(struct link));

node = node->next;

printf("\n Input the node: %d: ", (i+1));

scanf("%d", &node->data);

node->next = NULL;

fflush(stdin);

printf("\n Input choice n for break: ");

ch = getchar();

i++;

void insertion(struct link *node)

node = start.next;

previous = &start;

new1 = (struct link* ) malloc(sizeof(struct link));

new1->next = node ;

previous->next = new1;

printf("\n Input the fisrt node value: ");

scanf("%d", &new1->data);

void display(struct link *node)

node = start.next;

printf("\n After Inserting a node list is as follows:\n");

while (node)

{
printf(" %d\t", node->data);

node = node->next;

void main()

struct link *node = (struct link *) malloc(sizeof(struct link));

clrscr();

create_list(node);

insertion(node);

display(node);

getch();

Output
16. /* WAP of Deletion at the Last of Linked list*/

#include<stdio.h>

#include<conio.h>

#include<malloc.h>

typedef struct nodetype

int info;

struct nodetype *next;

} node ;

void createlist(node **,int );

void deleteatlast(node **);

void display(node *);

void main()

node *start;

int item,n,i;

clrscr();

start=NULL;

printf("Enter no. of nodes :\n");

scanf("%d",&n);

for(i= 0 ;i< n ;i++)

printf("Item for node %d :\n",i+1);

scanf("%d",&item);

createlist(&start,item) ;

}
printf("The list is :\n");

display(start);

printf("\nPress any key to delete last node");

getch();

deleteatlast(&start);

printf("\nThe list after the deletion at first is :\n");

display(start);

getch();

void createlist(node **start,int item)

node *ptr,*last;

ptr =(node*)malloc(sizeof(node));

ptr->info = item ;

ptr->next = NULL;

if(*start == NULL)

*start = ptr ;

else

last = *start ;

while(last->next !=NULL)

last = last->next;

last->next = ptr ;

}
void deleteatlast(node **start)

node *ptr,*cptr;

int temp;

if((*start)->next == NULL)

ptr = *start ;

*start = NULL;

temp = ptr->info;

else

cptr = *start ;

ptr =(*start)->next;

while(ptr->next != NULL)

cptr = ptr;

ptr = ptr->next;

cptr->next = NULL;

temp = ptr->info;

free(ptr);

printf("\nDeleted item is %d : \n",temp);

void display(node *start)

{ int n = 0;
while(start !=NULL)

printf("\t %d",start->info);

n++;

start = start->next;

printf("\n Total number of nodes : %d",n);

Output

You might also like