Shkti Dsa Final
Shkti Dsa Final
Table of Contents
Q1. WAP to search an element from a list . Give user the option to perform Linear or
Binary Search. .......................................................................................................................... 2
Q2. WAP using template to sort a list of elements . Give user the option to perform
sorting using Insertion sort , Bubble sort or Selection sort .................................................. 4
Q3. WAP using templates to sort a list of elements . Give user the option to perform
sorting using Merge sort and Quick sort .............................................................................. 7
Q4. WAP using templates to sort a list of elements . Give user the option to perform
sorting using Counting and radix sort . ................................................................................ 10
Q5. Implement Linked List using templates . Include functions for insertion , deletion and
search of a number , reverse the list and concatenate two linked lists . ........................... 13
Q6. Implement Doubly Linked List using templates . Include functions for insertion ,
deletion and search of a number , reverse of list . ............................................................... 21
Q7. Perform Stack operation using Linked List implementation . .................................... 27
Q8. Perform Stack operations using Array implementation . .............................................. 31
Q9. Perform Queue operations using Linked List implementation . ................................... 35
Q10. Perform Queue operations using Array implementation . ........................................... 39
Q11. Perform Queues operation using Circular Array implementation . ............................ 42
Q12. Create and perform different operations on Double-ended Queues using Linked List
implementation . ...................................................................................................................... 46
Q13. WAP to scan a polynomial using Linked List and add to polynomials . .............. 50
Q14. WAP to calculate factorial and to compute the factors of a given no. (i) using
recursion , (ii) using iteration . ............................................................................................... 53
Q15. WAP to display Fibonacci series (i) using recursion , (ii) using iteration . ............ 54
Q16. WAP to calculate GCD of 2 numbers (i) with recursion ,(ii) without recursion . .. 55
Q17. WAP to reverse the order of the elements in the stack using additional stack . ... 57
Q18. WAP to reverse the order of the elements in the stack using additional queue . ... 58
Q19. WAP to implement Lower Triangular Matrix using two-dimensional array. .......... 59
Q20. WAP to implement Symmetrix Matrix using two-dimensional array. ....................... 60
Page |2
Q1. WAP to search an element from a list . Give user the option to perform Linear or
Binary Search.
#include<stdio.h>
#include<stdlib.h>
for(int i=0;i<limit;i++)
scanf("%d",&a[i]); }
for(int i=0;i<limit;i++)
printf("%d ",a[i]); }
for(int i=0;i<limit;i++)
if(a[i]==value)
return i+1;
return (-1);
{ if(first<=limit)
{ int mid=(first+limit)/2;
if(a[mid]==value)
return mid;
else if(a[mid]>value)
return binary_search(a,first,mid-1,value);
else
return binary_search(a,mid+1,limit,value); }
return (-1); }
int main()
int limit,value,result,choice;
scanf("%d",&limit);
Page |3
int array[limit];
get(array,limit);
show(array,limit);
fflush(stdin);
scanf("%d",&value);
label:
scanf("%d",&choice);
switch(choice)
{ case 1: result=linear_search(array,limit,value);
break;
case 2: result=binary_search(array,0,limit-1,value);
break;
goto label; }
return 0;
}
Page |4
Q2. WAP using template to sort a list of elements . Give user the option to perform
sorting using Insertion sort , Bubble sort or Selection sort .
#include<stdio.h>
void swap(int *a,int *b)
{ int temp=*a;
*a=*b;
*b=temp; }
void get(int a[],int limit)
{ printf("Enter the elements : \n");
for(int i=0;i<limit;i++)
scanf("%d",&a[i]); }
void show(int a[],int limit)
{ printf("\nThe array is \n");
for(int i=0;i<limit;i++)
printf("%d ",a[i]); }
void bubble_sort(int a[],int limit)
{ for(int i=0;i<limit-1;i++)
for(int j=0;j<limit-1-i;j++)
if(a[j]>a[j+1])
swap(&a[j],&a[j+1]); }
void insertion_sort(int a[],int limit)
{ for(int i=1;i<limit;i++)
{ int key=a[i];
int j=i-1;
while(j>=0 && a[j]>key)
{ a[j+1]=a[j];
j=j-1; }
a[j+1]=key; } }
void selection_sort(int a[],int limit)
{ for(int i=0;i<limit;i++)
{ int min=i;
Page |5
for(int j=i+1;j<limit;j++)
if(a[j]<a[min])
min=j;
if(min!=i)
swap(&a[min],&a[i]); } }
int main()
{ int limit,choice;
printf("Enter the limit of array : ");
scanf("%d",&limit);
int array[limit];
get(array,limit);
show(array,limit);
fflush(stdin);
printf("\n\nChoose your option\n1. Bubble sort\n2. Insertion sort\n3. Selection sort\nEnter : ");
label:
scanf("%d",&choice);
switch(choice)
{ case 1: bubble_sort(array,limit);
break;
case 2: insertion_sort(array,limit);
break;
case 3: selection_sort(array,limit);
break;
default: printf("Please enter a valid choice...\nEnter : ");
goto label; }
printf("\nAfter sorting\n");
show(array,limit);
return 0;
}
Page |6
Page |7
Q3. WAP using templates to sort a list of elements . Give user the option to perform
sorting using Merge sort and Quick sort .
#include<stdio.h>
void swap(int *a,int *b)
{ int temp= *a;
*a=*b;
*b=temp; }
void get(int a[],int limit)
{ printf("Enter the elements : \n");
for(int i=0;i<limit;i++)
scanf("%d",&a[i]); }
void show(int a[],int limit)
{ printf("\nThe array is \n");
for(int i=0;i<limit;i++)
printf("%d ",a[i]); }
void merge(int a[],int beg,int mid,int end)
{ int n1=mid-beg+1;
int n2=end-mid;
int L[n1],R[n2];
for(int i=0;i<n1;i++)
L[i]=a[beg+i];
for(int j=0;j<n2;j++)
R[j]=a[mid+1+j];
int i=0,j=0,k=beg;
while(i<n1 && j<n2)
{ if(L[i]<R[j])
{ a[k]=L[i];
i++; }
else {
a[k]=R[j];
j++; }
Page |8
k++; }
while(i<n1)
{ a[k]=L[i];
i++;
k++; }
while(j<n2)
{ a[k]=R[j];
j++;
k++; } }
void merge_sort(int a[],int beg,int end)
{ if(beg<end)
{ int mid=(beg+end)/2;
merge_sort(a,beg,mid);
merge_sort(a,mid+1,end);
merge(a,beg,mid,end); } }
int partition(int a[],int beg,int end)
{ int pivot=a[end];
int i= beg-1;
for(int j=beg;j<end;j++)
{ if(a[j]<pivot)
{ i++;
swap(&a[i],&a[j]); } }
swap(&a[i+1],&a[end]);
return i+1; }
void quick_sort(int a[],int beg,int end)
{ if(beg<end)
{ int pi=partition(a,beg,end);
quick_sort(a,beg,pi-1);
quick_sort(a,pi+1,end); } }
int main()
Page |9
{ int limit,choice;
printf("Enter the limit of array : ");
scanf("%d",&limit);
int array[limit];
get(array,limit);
show(array,limit);
fflush(stdin);
printf("\n\nChoose your option\n1. Merge sort\n2. Quick sort\nEnter : ");
label:
scanf("%d",&choice);
switch(choice)
{ case 1: merge_sort(array,0,limit-1);
break;
case 2: quick_sort(array,0,limit-1);
break;
default: printf("Please enter a valid choice...\nEnter : ");
goto label; }
printf("\nAfter sorting\n");
show(array,limit);
return 0; }
P a g e | 10
Q4. WAP using templates to sort a list of elements . Give user the option to perform
sorting using Counting and radix sort .
#include<stdio.h>
int get_max(int a[],int limit)
{ int m=a[0];
for(int i=1;i<limit;i++)
if(a[i]>m)
m=a[i];
return m; }
void get(int a[],int limit)
{ printf("Enter the elements : \n");
for(int i=0;i<limit;i++)
scanf("%d",&a[i]); }
void show(int a[],int limit)
{ printf("\nThe array is \n");
for(int i=0;i<limit;i++)
printf("%d ",a[i]); }
void count_sort(int a[],int limit)
{ int output[limit+1];
int max=get_max(a,limit);
int count[max+1];
for(int i=0;i<=max;i++)
count[i]=0;
for(int i=0;i<limit;i++)
count[a[i]]++;
for(int i=1;i<=max;i++)
count[i]+=count[i-1];
for(int i=limit-1;i>=0;i--)
{ output[count[a[i]]-1]=a[i];
count[a[i]]--; }
for(int i=0;i<limit;i++)
P a g e | 11
a[i]=output[i]; }
void count(int a[],int limit,int place)
{ int output[limit+1];
int count[10]={0};
for(int i=0;i<limit;i++)
count[(a[i]/place)%10]++;
for(int i=1;i<10;i++)
count[i]+=count[i-1];
for(int i=limit-1;i>=0;i--)
{ output[count[(a[i]/place)%10]-1]=a[i];
count[(a[i]/place)%10]--; }
for(int i=0;i<limit;i++)
a[i]=output[i]; }
void radix_sort(int a[],int limit)
{ int max=get_max(a,limit);
for(int place=1;(max/place)>0;place*=10)
count(a,limit,place); }
int main()
{ int limit,choice;
printf("Enter the limit of array : ");
scanf("%d",&limit);
int array[limit];
get(array,limit);
show(array,limit);
fflush(stdin);
printf("\n\nChoose your option\n1. Counting sort\n2. Radix sort\nEnter : ");
label:
scanf("%d",&choice);
switch(choice)
{ case 1: count_sort(array,limit);
P a g e | 12
break;
case 2: radix_sort(array,limit);
break;
default: printf("Please enter a valid choice...\nEnter : ");
goto label; }
printf("\nAfter sorting\n");
show(array,limit);
return 0;
}
P a g e | 13
Q5. Implement Linked List using templates . Include functions for insertion , deletion
and search of a number , reverse the list and concatenate two linked lists .
#include<stdio.h>
#include<stdlib.h>
struct node
{ int data;
struct node *link; };
struct node *start,*ptr,*pre,*cur;
int choice,count=1,pos,element;
void creation()
{ ptr=(struct node *)malloc(sizeof(struct node));
printf("Create a list\nEnter the data : ");
scanf("%d",&ptr->data);
ptr->link=NULL;
start=ptr;
printf("Do you want to add another data ? (Yes/No=0) : ");
scanf("%d",&choice);
while(choice!=0)
{ struct node *nptr;
nptr=(struct node *)malloc(sizeof(struct node));
printf("Enter the data : ");
scanf("%d",&nptr->data);
nptr->link=NULL;
ptr->link=nptr;
count++;
printf("Do you want to add another data ? (Yes/No=0) : ");
scanf("%d",&choice);
ptr=nptr; } }
void list()
{ printf("\nThe list is \n");
ptr=start;
P a g e | 14
while(ptr!=NULL)
{ printf("|%d|->",ptr->data);
ptr=ptr->link; } }
void insertion()
{ printf("Enter the position where you want to add data : ");
label:
scanf("%d",&pos);
if(pos>=count+2)
{ printf("Please enter a valid position...\nEnter : ");
goto label; }
else
{ struct node *next;
next=(struct node *)malloc(sizeof(struct node));
printf("Enter the data : ");
scanf("%d",&next->data);
if(pos==1)
{ next->link=start;
start=next; }
else
{ int i=1;
ptr=start;
while(i!=pos-1)
{ ptr=ptr->link;
i++; }
next->link=ptr->link;
ptr->link=next; } } }
void deletion()
{ printf("Enter the data you want to delete : ");
scanf("%d",&pos);
ptr=start;
P a g e | 15
int i=1;
if(pos==ptr->data)
{ struct node *temp=start;
start=start->link;
free(temp); }
else
{ while(ptr!=NULL)
{ i++;
if(pos==ptr->link->data)
break;
ptr=ptr->link; } }
if(i>count)
printf("Element does not exist...");
else
{ struct node *temp=ptr->link;
ptr->link=ptr->link->link;
free(temp); } }
void reverse(struct node *pre,struct node *cur)
{ if(cur)
{ reverse(cur,cur->link);
cur->link=pre; }
else
start=pre; }
int search()
{ printf("Enter the element you want to search : ");
scanf("%d",&pos);
ptr=start;
int i=1;
while(ptr!=NULL)
{ if(pos==ptr->data)
P a g e | 16
return i;
ptr=ptr->link;
i++; }
return (-1); }
void concatenate()
{ struct node *start1,*start2;
printf("Create first list\n");
ptr=(struct node *)malloc(sizeof(struct node));
printf("Enter the data : ");
scanf("%d",&ptr->data);
ptr->link=NULL;
start1=ptr;
printf("Do you want to add another data ? (Yes/No=0) : ");
scanf("%d",&choice);
while(choice!=0)
{ struct node *nptr;
nptr=(struct node *)malloc(sizeof(struct node));
printf("Enter the data : ");
scanf("%d",&nptr->data);
nptr->link=NULL;
ptr->link=nptr;
printf("Do you want to add another data ? (Yes/No=0) : ");
scanf("%d",&choice);
ptr=nptr; }
printf("Create the second list \n");
struct node *mptr;
mptr=(struct node *)malloc(sizeof(struct node));
printf("Enter the data : ");
scanf("%d",&mptr->data);
mptr->link=NULL;
P a g e | 17
start2=mptr;
printf("Do you want to add another data ? (Yes/No=0) : ");
scanf("%d",&choice);
while(choice!=0)
{ struct node *nptr;
nptr=(struct node *)malloc(sizeof(struct node));
printf("Enter the data : ");
scanf("%d",&nptr->data);
nptr->link=NULL;
mptr->link=nptr;
printf("Do you want to add another data ? (Yes/No=0) : ");
scanf("%d",&choice);
mptr=nptr; }
ptr->link=start2;
printf("\nAfter concatenate\nThe list is\n");
ptr=start1;
while(ptr!=NULL)
{ printf("|%d|->",ptr->data);
ptr=ptr->link; } }
int main()
{ creation();
list();
label:
printf("\n\nChoose your option\n1. Insertion\n2. Deletion\n3. Search of element\n4. Reverse\n5.
Concatenate\nEnter : ");
scanf("%d",&choice);
switch(choice)
{ case 1: insertion();
list();
break;
case 2: deletion();
P a g e | 18
list();
break;
case 3: element=search();
element == -1 ? printf("\nElement does not exist") : printf("\nElement is at position %d",element);
break;
case 4: cur=start;
pre=NULL;
reverse(pre,cur);
printf("\nAfter reversing \n");
list();
break;
case 5: concatenate();
break;
default: printf("Please enter a valid option...\n\n");
goto label; }
printf("\n\nDo you want to perform any other operation ? (Yes/No=0) : ");
scanf("%d",&choice);
if(choice!=0)
goto label;
else
printf("...Thanks for visiting...");
return 0;
}
P a g e | 19
P a g e | 20
P a g e | 21
Q6. Implement Doubly Linked List using templates . Include functions for insertion ,
deletion and search of a number , reverse of list .
#include<stdio.h>
#include<stdlib.h>
struct node
{ int data;
struct node *link;
struct node *prev; };
struct node *start,*end,*ptr;
int choice,count=1,pos,element;
void creation()
{ printf("Create a list\n");
ptr=(struct node *)malloc(sizeof(struct node));
printf("Enter the data : ");
scanf("%d",&ptr->data);
ptr->prev=NULL;
ptr->link=NULL;
start=ptr;
printf("Do you want to add another element ? (Yes/No=0) : ");
scanf("%d",&choice);
while(choice!=0)
{ struct node *nptr;
nptr=(struct node *)malloc(sizeof(struct node));
printf("Enter the data : ");
scanf("%d",&nptr->data);
nptr->link=NULL;
nptr->prev=ptr;
ptr->link=nptr;
count++;
printf("Do you want to add another element ? (Yes/No=0) : ");
scanf("%d",&choice);
P a g e | 22
ptr=nptr; }
end=ptr; }
void list()
{ count=0;
printf("\nThe list is\n");
ptr=start;
while(ptr!=NULL)
{ printf("|%d| <-> ",ptr->data);
count++;
ptr=ptr->link; } }
void insertion()
{ printf("Enter the position where you want to add element : ");
label:
scanf("%d",&pos);
if(pos>=count+2)
{ printf("Please enter a valid position....\nEnter : ");
goto label; }
else
{ struct node *next;
next=(struct node *)malloc(sizeof(struct node));
printf("Enter the data : ");
scanf("%d",&next->data);
if(pos==1)
{ next->link=start;
next->prev=NULL;
start->prev=next;
start=next; }
else if(pos==count+1)
{ ptr=start;
while(ptr->link!=NULL)
P a g e | 23
{ ptr=ptr->link; }
next->link=NULL;
next->prev=ptr;
ptr->link=next;
end=next; }
else
{ int i=1;
ptr=start;
while(i!=pos-1)
{ ptr=ptr->link;
i++; }
next->link=ptr->link;
next->prev=ptr->link->prev;
ptr->link->prev=next;
ptr->link=next; } }
list(); }
void deletion()
{ printf("Enter the element you want to delete : ");
scanf("%d",&pos);
if(pos==start->data)
{ struct node *t;
t=start;
start=start->link;
start->prev=NULL;
free(t); }
else
{ ptr=start;
while(ptr!=NULL)
{ if(pos==ptr->link->data)
break;
P a g e | 24
ptr=ptr->link; }
if(ptr==NULL)
printf("Element does not exist");
else if(ptr->link->link==NULL)
{ struct node *t;
t=ptr->link;
ptr->link=NULL;
free(t); }
else
{ struct node *t;
t=ptr->link;
ptr->link=ptr->link->link;
ptr->link->prev=ptr;
free(t); } }
list(); }
int search()
{ printf("Enter the element you want to search : ");
scanf("%d",&pos);
ptr=start;
int i=1;
while(ptr!=NULL)
{ if(pos==ptr->data)
return i;
ptr=ptr->link;
i++; }
return (-1); }
void reverse()
{ ptr=end;
while(ptr!=NULL)
{ printf("|%d| <-> ",ptr->data);
P a g e | 25
ptr=ptr->prev; } }
int main()
{ creation();
list();
label:
printf("\n\nChoose your operation\n1. Insertion\n2. Deletion\n3. Search for a element\n4. Reverse\nEnter : ");
scanf("%d",&choice);
switch(choice)
{ case 1: insertion();
break;
case 2: deletion();
break;
case 3: element=search();
element == -1 ? printf("\nElement does not exist...\n") : printf("\nElement is at position %d\n",element) ;
break;
case 4: reverse();
break;
default: printf("\nPlease enter a valid choice...\n");
goto label; }
printf("\n\nDo you want to perform any other operation ? (Yes/No=0) : ");
scanf("%d",&choice);
if(choice!=0)
goto label;
else
printf("...Thanks for visiting...");
return 0;
}
P a g e | 26
P a g e | 27
start=start->link;
free(temp); } }
int main()
{ stack();
label:
printf("\n\nChoose your operation\n1. Push\n2. Pop\nEnter : ");
scanf("%d",&choice);
switch(choice)
{ case 1: push();
stack();
break;
case 2: pop();
stack();
break;
default: printf("\nPlease enter a valid choice...\n");
goto label; }
printf("\nDo you want to perform another operation ? (Yes/No=0) : ");
scanf("%d",&choice);
if(choice!=0)
goto label;
else
printf("...Thanks for visiting...");
return 0;
}
P a g e | 29
P a g e | 30
P a g e | 31
switch(choice)
{ case 1: push();
break;
case 2: pop();
break;
default: printf("\nPlease enter a valid choice...\n");
goto label; }
printf("\nDo you want to perform any other operation ? (Yes/No=0) : ");
scanf("%d",&choice);
if(choice!=0)
goto label;
else
printf("...Thanks for visiting...");
return 0;
}
P a g e | 33
P a g e | 34
P a g e | 35
void dequeue()
{ if(start==NULL)
printf("...Underflow...");
else if(start->link==NULL)
start=NULL;
else
{ struct node *t;
t=start;
start=start->link;
free(t); }
queue(); }
int main()
{ queue();
label:
printf("\nChoose the operation you want to perform\n1. Enqueue\n2. Dequeue\nEnter : ");
scanf("%d",&choice);
switch(choice)
{ case 1: enqueue();
break;
case 2: dequeue();
break;
default: printf("Please enter a valid choice...\n");
goto label; }
printf("\nDo you want to perform any other operation ? (Yes/No=0) : ");
scanf("%d",&choice);
if(choice!=0)
goto label;
else
printf("...Thanks for visiting...");
return 0;
P a g e | 37
}
P a g e | 38
P a g e | 39
{ queue();
label:
printf("\nChoose
nChoose the operation you want to perform\n1.
perform Enqueue\n2. Dequeue\nEnter
nEnter : ");
scanf("%d",&choice);
switch(choice)
{ case 1: enqueue();
break;
case 2: dequeue();
break;
default: printf("\nPlease
nPlease enter a valid choice...
choice...\n");
goto label; }
printf("\nDo
nDo you want to perform any other opertion ? (Yes/No=0) : ");
scanf("%d",&choice);
if(choice!=0)
goto label;
else
printf("...Thanks for visiting...");
return 0; }
P a g e | 41
P a g e | 42
if(front==rear)
front=rear=-1;
else
{ if(front==3)
front=0;
else
front++; }
printf("front : %d",front);
queue(); }
int main()
{ queue();
label:
printf("\nChoose the operation you want to perform\n1. Enqueue\n2. Dequeue\nEnter : ");
scanf("%d",&choice);
switch(choice)
{ case 1: enqueue();
break;
case 2: dequeue();
break;
default: printf("\nPlease enter a valid choice...\n");
goto label; }
printf("\nDo you want to perform any other opertion ? (Yes/No=0) : ");
scanf("%d",&choice);
if(choice!=0)
goto label;
else
printf("...Thanks for visiting...");
return 0;
}
P a g e | 44
P a g e | 45
P a g e | 46
Q12. Create and perform different operations on Double-ended Queues using Linked List
implementation .
#include <stdio.h>
#include <stdlib.h>
struct node
{ int data;
void deque()
printf(" |");
else
{ ptr = start;
ptr = ptr->link; }
ptr = end;
ptr = ptr->link; } } }
void insertion_at_front()
scanf("%d", &front->data);
front->link = NULL;
if (start == NULL)
start = front;
else
{ ptr = start;
{ ptr = ptr->link; }
ptr->link = front; }
deque(); }
P a g e | 47
void insertion_at_rear()
scanf("%d", &rear->data);
rear->link = end;
end = rear;
deque(); }
void deletion_at_front()
printf("...Underflow...");
else
ptr = start;
if(ptr->link==NULL)
{ temp=start;
start=NULL;
free(temp); }
else
{ ptr = ptr->link; }
temp = ptr->link;
ptr->link = NULL;
free(temp); } }
deque(); }
void deletion_at_rear()
printf("...Underflow...");
else
ptr = end;
if(end!=NULL)
{ temp=end;
end=end->link;
free(temp); } }
P a g e | 48
deque(); }
int main()
{ int choice;
label:
scanf("%d", &choice);
switch (choice)
{ case 1: insertion_at_front();
break;
case 2: insertion_at_rear();
break;
case 3: deletion_at_front();
break;
case 4: deletion_at_rear();
break;
default:
goto label; }
scanf("%d", &choice);
if (choice != 0)
goto label;
else
return 0; }
P a g e | 49
P a g e | 50
Q13. WAP to scan a polynomial using Linked List and add to polynomials .
#include<stdio.h>
#include<stdlib.h>
struct node
{ int coeff;
struct node *link; };
struct node *head=NULL,*ptr,*nptr;
int power1,power2;
struct node* create(struct node *head,int power)
{ while(power!=-1)
{ struct node *next=(struct node *)malloc(sizeof(struct node));
printf("Enter the coefficient of x^%d : ",power);
scanf("%d",&next->coeff);
next->link=NULL;
if (head==NULL)
head=next;
else
{ ptr=head;
while(ptr->link!=NULL)
ptr=ptr->link;
ptr->link=next; }
power--; }
return head; }
void list(struct node *head,int power)
{ ptr=head;
while(ptr!=NULL)
{ printf("%d x^%d ",ptr->coeff,power);
ptr=ptr->link;
if(power!=0 && ptr->coeff>0)
printf("+ ");
power--; } }
P a g e | 51
list(head1,power1);
printf("\nThe second polynomial is\n");
list(head2,power2);
printf("\n\nAfter addition of polynomial\nThe polynomial is\n");
add(head1,head2,power1,power2);
return 0; }
P a g e | 53
Q14. WAP to calculate factorial and to compute the factors of a given no. (i) using
recursion , (ii) using iteration .
#include<stdio.h>
{ if(num==1)
return 1;
else
return num*factorial(num-1); }
int main()
{ int num,choice,result=1;
printf("\t...Find the factorial and factors of a number...\n Enter the number : ");
scanf("%d",&num);
label:
printf("Choose the operation you want to perform\n1. Iteration method\n2. Recursion\nEnter : ");
scanf("%d",&choice);
switch(choice)
result*=i;
break;
case 2: result=factorial(num);
break;
goto label; }
for(int i=2;i<=num/2;i++)
if(num%i==0)
printf("%d ",i);
return 0; }
P a g e | 54
Q15. WAP to display Fibonacci series (i) using recursion , (ii) using iteration .
#include <stdio.h>
{ if (num <= 1)
return num;
else
int main()
printf("\t...Fibonacci Series...\n");
scanf("%d", &limit);
label:
scanf("%d", &choice);
switch (choice)
printf("%d,", fibonacci(i));
printf("...");
break;
printf("...");
break;
goto label; }
return 0; }
P a g e | 55
Q16. WAP to calculate GCD of 2 numbers (i) with recursion ,(ii) without recursion .
#include <stdio.h>
int gcd(int num1, int num2)
{ if (num1 == 0)
return num2;
if (num2 == 0)
return num1;
if (num1 == num2)
return num1;
if (num1 > num2)
return gcd(num1 - num2, num2);
return gcd(num1, num2 - num1); }
int main()
{ int num1, num2, choice, result;
printf("\t...Find the GCD of two numbers...\n");
printf("Enter the first number : ");
scanf("%d", &num1);
printf("Enter the second number : ");
scanf("%d", &num2);
label:
printf("Choose the operation you want to perform\n1. Recusrion\n2. Without Recursion\nEnter : ");
scanf("%d", &choice);
switch (choice)
{
case 1: result = gcd(num1, num2);
break;
case 2: for (int i = 1; i < (num1 > num2 ? num1 : num2); i++)
if (num1 % i == 0 && num2 % i == 0)
result = i;
break;
default: printf("Please enter a valid choice...\n");
P a g e | 56
goto label; }
printf("\nThe GCD of %d and %d is %d", num1, num2, result);
return 0; }
P a g e | 57
Q17. WAP to reverse the order of the elements in the stack using additional stack .
#include <stdio.h>
int stack[4], reverse_stack[4];
int main()
{ printf("\t...Reversing order of elements in a stack using additional stack...\n");
printf("Create a stack (size of stack is 4)\n");
printf("Push the elemnts in stack : ");
for (int i = 0; i < 4; i++)
scanf("%d", &stack[i]);
printf("\nThe stack is\n");
for (int i = 0, j = 3; j >= 0; i++, j--)
{ printf("|_%2d_|\n", stack[j]);
reverse_stack[i] = stack[j]; }
for (int i = 0; i < 4; i++)
stack[i] = reverse_stack[i];
printf("\nThe stack with reverse order of elements is\n");
for (int i = 3; i >= 0; i--)
printf("|_%2d_|\n", stack[i]);
return 0;
}
P a g e | 58
Q18. WAP to reverse the order of the elements in the stack using additional queue .
#include <stdio.h>
int stack[4], queue[4];
int main()
{ printf("...Reversing the order of elements in stack using additional queue...\n");
printf("Create a stack (size of stack is 4)\n");
printf("Push the elements in stack : ");
for (int i = 0; i < 4; i++)
scanf("%d", &stack[i]);
printf("\nThe stack is\n");
for (int i = 3, j = 0; i >= 0; i--, j++)
{ printf("|_%2d_|\n", stack[i]);
queue[j] = stack[i]; }
for (int i = 3; i >= 0; i--)
stack[i] = queue[i];
printf("\nAfter reversing the order of elements the stack is\n");
for (int i = 3; i >= 0; i--)
printf("|_%2d_|\n", stack[i]);
return 0; }
P a g e | 59
int main()
scanf("%d", &rows);
scanf("%d", &columns);
int matrix[rows][columns];
scanf("%d", &matrix[i][j]);
{ printf("|");
printf("%2d", matrix[i][j]);
printf(" |\n"); }
{ if (i < j)
continue;
else
printf("\n"); }
return 0; }
P a g e | 60
if (matrix[i][j] != matrix[j][i])
return 1;
return (-1); }
int main()
scanf("%d", &rows);
scanf("%d", &columns);
int matrix[rows][columns];
scanf("%d", &matrix[i][j]);
{ printf("| ");
printf("|\n"); }
return 0; }