PROBLEM STATEMENT:-20 Design, Develop and Implement a menu driven
Program in C for the following operations on Circular QUEUE of Characters (Array
Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situationson Circular QUEUE
d. Display the current status of Circular QUEUE
e. Exit
ALGORITHM:-
1. Insert (Enqueue) : Add an element to the rear of the queue.
2. Delete (Dequeue) : Remove an element from the front of the queue.
3. Overflow : Occurs when trying to add an element to a full queue.
4. Underflow : Occurs when trying to remove an element from an empty queue.
5. Display : Shows all elements in the queue.
6. Current Status : Displays the status of the queue including front and rear
pointers.
7. Exit : Exit the program.
SOURCE CODE:-
#include <stdio.h>
#include <stdlib.h>
#define max 3
typedef struct {
char queue[max];
int front;
int rear;
} circularq
void initializequeue(circularqueue *cq) {
cq->front = -1;
cq->rear = -1;
int isempty(circularqueue *cq) {
return (cq->front =
int isfull(circularqueue *cq) {
return ((cq->rear + 1) % max == cq->front);
void enqueue(circularqueue *cq, char element) {
if (isfull(cq)) {
printf(“queue overflow! cannot insert ‘%c’\n”, element);
return;
if (isempty(cq)) {
cq->front = 0;
cq->rear = (cq->rear + 1) % max;
cq->queue[cq->rear] = element;
printf(“inserted ‘%c’ into the queue\n”, element);
char dequeue(circularqueue *cq) {
if (isempty(cq)) {
printf(“queue underflow! cannot delete element\n”);
return ‘\0’;
}
char element = cq->queue[cq->front];
if (cq->front == cq->rear) {
cq->front = cq->rear = -1; // queue is now empty
} else {
cq->front = (cq->front + 1) % max;
printf(“deleted ‘%c’ from the queue\n”, element);
return element
void displayqueue(circularqueue *cq) {
if (isempty(cq)) {
printf(“queue is empty\n”);
return;
int i = cq->front;
printf(“queue elements: “);
while (1) {
printf(“%c “, cq->queue[i]);
if (i == cq->rear) break;
i = (i + 1) % max;
printf(“\n”);
void currentstatus(circularqueue *cq) {
printf(“queue status:\n”);
printf(“front: %d\n”, cq->front);
printf(“rear: %d\n”, cq->rear);
displayqueue(cq);
int main() {
circularqueue cq;
initializequeue(&cq);
int choice;
char element;
while (1) {
printf(“\ncircular queue operations:\n”);
printf(“1. enqueue\n”);
printf(“2. dequeue\n”);
printf(“3. display\n”);
printf(“4. current status\n”);
printf(“5. exit\n”);
printf(“enter your choice: “);
scanf(“%d”, &choice);
switch (choice) {
case 1:
printf(“enter an element to insert: “);
scanf(“ %c”, &element);
enqueue(&cq, element);
break;
case 2:
dequeue(&cq);
break;
case 3:
displayqueue(&cq);
break;
case 4:
currentstatus(&cq);
break;
case 5:
exit(0);
default:
printf(“invalid choice! please enter a valid option.\n”);
return 0;
}
OUTPUT:-
PROBLEM STATEMENT:-21 Develop a C program that implements a linear search
algorithm to find the position of a given element in an array.
Test case 1:
LINEAR SEARCH
Enter the how many values you want to enter-:3
Enter 1 no-:25
Enter 2 no-:26
Enter 3 no-:27
Enter the no you want to do LINEAR SEARCH on-:26
The no 26 is present in array at position 2
ALGORITHM:-
1. Start
2. Input the number of elements (`n`) in the array.
3. Declare an array of size `n`.
4. Input `n` elements into the array.
5. Input the target element to search for.
6. Iterate through the array from the first element to the last:
- If the current element is equal to the target element:
- Return the current position (1-indexed).
7. If the target element is not found after the iteration, return `-1`.
8. End
SOURCE CODE:-
#include <stdio.h>
int linearsearch(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i + 1; // returning position (1-indexed)
return -1; // element not found
int main() {
int n, target, position;
printf(“linear search\n”);
printf(“enter how many values you want to enter-: “);
scanf(“%d”, &n);
int arr[n];
for (int i = 0; i < n; i++) {
printf(“enter %d no-: “, i + 1);
scanf(“%d”, &arr[i]);
printf(“enter the no you want to do linear search on-: “);
scanf(“%d”, &target);
position = linearsearch(arr, n, target);
if (position != -1) {
printf(“the no %d is present in array at position %d\n”, target, position);
} else {
printf(“the no %d is not present in the array\n”, target);
printf(“\n Program created by \n Name: Rishabh Bisht\n Course: BCA-
II(F2 Section)\n Roll No.: 46\n”);
return 0;
OUTPUT:-
PROBLEM STATEMENT :-22 Develop a C program that implements a Binary
search algorithm to find the position of a given element in an array.
Test case 1:
BINARY SEARCH
Enter the how many values you want to enter-:3
Enter 1 no-:35
Enter 2 no-:36
Enter 3 no-:37
Enter the no you want to do BINARY SEARCH on-:36
The no 36 is present in array at position 2
ALGORITHM:-
1. Input:
- Prompt the user to enter the number of elements `n`
- Create an array `arr` of size `n`
- Prompt the user to enter each element of the array
- Prompt the user to enter the target element `target`
2. Call the Binary Search Function:
- Pass the array `arr`, its size `n`, and the target element `target` to the
`binarySearch` function
- Store the result of the function call
3. Output:
- If the result is not `-1`, print the position of the target element
- If the result is `-1`, print that the target element is not present in the array
SOURCE CODE:-
#include <stdio.h>
int binarysearch(int arr[], int size, int target) {
int left = 0;
int right = size – 1;
while (left <= right) {
int mid = left + (right – left) / 2
if (arr[mid] == target)
return mid + 1; // +1 to convert 0-based index to 1-based position
if (arr[mid] < target)
left = mid + 1;
else
right = mid – 1;
return -1;
int main() {
int n, target, result;
printf(“binary search\n”);
printf(“enter the how many values you want to enter-: “);
scanf(“%d”, &n);
int arr[n];
for (int i = 0; i < n; i++) {
printf(“enter %d no-: “, i + 1);
scanf(“%d”, &arr[i]);
}
printf(“enter the no you want to do binary search on-: “);
scanf(“%d”, &target)
result = binarysearch(arr, n, target);
if (result != -1)
printf(“the no %d is present in array at position %d\n”, target, result);
else
printf(“the no %d is not present in the array\n”, target);
printf(“\n Program created by \n Name: Rishabh Bisht\n Course: BCA-II(F2
Section)\n Roll No.: 46\n”);
return 0;
OUTPUT:-
PROBLEM STATEMENT 23:-Develop a C program that implements a Selection
sorting algorithm to sort the elements in an array.
Test case 1:
SELECTION SORTING
Enter the how many values you want to enter-:5
Enter 1 no-:5
Enter 2 no-:3
Enter 3 no-:4
Enter 4 no-:1
Enter 5 no-:2
Array before sorting5,3,4,1,2,After Selection Sorting1,2,3,4,5,
ALGORITHM:-
1. Input:
- Prompt the user to enter the number of elements `n`.
- Create an array `arr` of size `n`.
- Prompt the user to enter each element of the array.
2. Process:
- Print the array before sorting.
- Call the `selectionSort` function to sort the array.
3. Output:
- Print the array after sorting.
SOURCE CODE:-
#include <stdio.h>
// function to perform selection sort
void selectionsort(int arr[], int n) {
int i, j, minindex, temp;
for (i = 0; i < n-1; i++) {
// find the minimum element in the unsorted part of the array
minindex = i;
for (j = i+1; j < n; j++) {
if (arr[j] < arr[minindex]) {
minindex = j;
// swap the found minimum element with the first element
temp = arr[minindex];
arr[minindex] = arr[i];
arr[i] = temp;
int main() {
int n;
printf(“selection sorting\n”);
printf(“enter the how many values you want to enter-: “);
scanf(“%d”, &n);
int arr[n];
for (int i = 0; i < n; i++) {
printf(“enter %d no-: “, i + 1);
scanf(“%d”, &arr[i]);
printf(“array before sorting\n”);
for (int i = 0; i < n; i++) {
printf(“%d”, arr[i]);
if (i < n-1) {
printf(“,”);
printf(“\n”);
selectionsort(arr, n);
printf(“after selection sorting\n”);
for (int i = 0; i < n; i++) {
printf(“%d”, arr[i]);
if (i < n-1) {
printf(“,”);
printf(“\n”);
printf(“the no %d is not present in the array\n”, target);
printf(“\n program created by \n name: rishabh bisht\n course: bca-ii(f2
section)\n roll no.: 46\n”);
return 0;
OUTPUT:-
PROBLEM STATEMENT:-Develop a C program that implements a Bubble sorting
algorithm to sort the elements in an array
Test case 1:BUBBLE SORTING
Enter the how many values you want to enter-:5
Enter 1 no-:55
Enter 2 no-:33
Enter 3 no-:44
Enter 4 no-:11
Enter 5 no-:22
Array before sorting55,35,44,11,22,After Bubble Sorting11,22,33,44,55,
ALGORITHM:-
SOURCE CODE:-
#include <stdio.h>
// function to perform bubble sort
void bubblesort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
// swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
int main() {
int n;
printf(“bubble sorting\n”);
printf(“enter the how many values you want to enter-: “);
scanf(“%d”, &n);
int arr[n];
for (int i = 0; i < n; i++) {
printf(“enter %d no-: “, i + 1);
scanf(“%d”, &arr[i]);
printf(“array before sorting\n”);
for (int i = 0; i < n; i++) {
printf(“%d”, arr[i]);
if (i < n-1) {
printf(“,”);
printf(“\n”)
bubblesort(arr, n);
printf(“after bubble sorting\n”);
for (int i = 0; i < n; i++) {
printf(“%d”, arr[i]);
if (i < n-1) {
printf(“,”);
printf(“\n”);
printf(“\n program created by \n name: rishabh bisht\n course: bca-ii(f2
section)\n roll no.: 46\n”);
return 0;
OUTPUT:-
PROBLEM STATEMENT:-25 Develop a C program that implements a Insertion
sorting algorithm to sort the elements in an array.
Test case 1:
INSERTION SORTING
Enter the how many values you want to enter-:5
Enter 1 no-:22
Enter 2 no-:23
Enter 3 no-:20
Enter 4 no-: 21
Enter 5 no-:24
Array before sorting22,23,20,21,24,
After Insertion Sorting20,21,22,23,24
ALGORITHM:-
1. Input:
- Prompt the user to enter the number of elements `n`.
- Create an array `arr` of size `n`.
- Prompt the user to enter each element of the array.
2. Process:
- Print the array before sorting.
- Call the `insertionSort` function to sort the array.
3. Output:
- Print the array after sorting
SOURCE CODE:-
#include <stdio.h>
void insertionsort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i – 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j – 1;
arr[j + 1] = key;
int main() {
int n;
printf(“insertion sorting\n”);
printf(“enter the how many values you want to enter-: “);
scanf(“%d”, &n);
int arr[n];
for (int i = 0; i < n; i++) {
printf(“enter %d no-: “, i + 1);
scanf(“%d”, &arr[i]);
printf(“array before sorting\n”);
for (int i = 0; i < n; i++) {
printf(“%d”, arr[i]);
if (i < n-1) {
printf(“,”);
}
}
printf(“\n”);
insertionsort(arr, n);
printf(“after insertion sorting\n”);
for (int i = 0; i < n; i++) {
printf(“%d”, arr[i]);
if (i < n-1) {
printf(“,”);
printf(“\n”);
printf(“\n program created by \n name: rishabh bisht\n course: bca-ii(f2
section)\n roll no.: 46”)\n
return 0;
OUTPUT:-