You are on page 1of 53

1a

#include<stdio.h>

#include<conio.h>

void main()

int num, choice, i, se, j;

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

scanf("%d", &num);

int arr[num];

printf("\nEnter array elements: ");

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

printf("\nEnter element %d: ", i);

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

printf("\nEntered elements are: ");

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

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

printf("\n_______________________________");

printf("\n1: Searching");

printf("\n2: Sorting");

printf("\n3: Reversing");

printf("\n4: Quit");
printf("\n_______________________________");

printf("\nChoose an option: ");

scanf("%d", &choice);

switch(choice)

case 1:

printf("\nEnter element to search: ");

scanf("%d", &se);

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

if(se == arr[i])

printf("Element found at index %d :", i);

break;

if(i == num)

printf("Element not found");

break;

case 2:

printf("\nSorting:");

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

for(j = 0; j < num-i-1; j++)


{

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

int temp = arr[j];

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

arr[j+1] = temp;

printf("\nSorted Array:");

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

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

break;

case 3:

printf("\nReversing:");

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

for(j = 0; j < num-i-1; j++)

if(arr[j] < arr[j+1])

int temp = arr[j];

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

arr[j+1] = temp;

}
}

printf("\nReversed Array:");

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

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

break;

case 4:

printf("Exiting program");

break;

default:

printf("Invalid choice");

break;

getch();

1b

#include<stdio.h>

int main() {

int a,b,i,j,k;

int temp;

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

scanf("%d",&a);

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

scanf("%d",&b);
int arr1[a],arr2[b],merged[a+b];

printf("Elements for array 1: ");

for(i=0;i<a;i++) {

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

printf("Elements for array 2: ");

for(i=0;i<b;i++) {

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

i=j=k=0;

while(i<a && j<b) {

if(arr1[i]<arr2[j]) {

merged[k++]=arr1[i++];

else {

merged[k++]=arr2[j++];

while(i<a) {

merged[k++]=arr1[i++];

while(j<b) {
merged[k++]=arr2[j++];

// Bubble Sort

for(i=0;i<a+b-1;i++) {

for(j=0;j<a+b-i-1;j++) {

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

temp = merged[j];

merged[j]= merged[j+1];

merged[j+1] = temp;

printf("\nMerged and sorted array: ");

for(i=0;i<a+b;i++) {

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

printf("\n");

return 0;

1c

#include<stdio.h>

void main()

int a[3][3];

int b[3][3];
int c[3][3];

int i,j,ch;

printf("Enter elements for array 1:");

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

for(j=0;j<3;j++)

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

printf("Enter elements for array 2:");

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

for(j=0;j<3;j++)

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

printf("Array 1:");

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

for(j=0;j<3;j++)

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

}printf("\n");

printf("Array 2:");
for(i=0;i<3;i++)

for(j=0;j<3;j++)

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

}printf("\n");

printf("\n_______________________________");

printf("\n1: Addition");

printf("\n2: Multiplication");

printf("\n3: Transpose");

printf("\n_______________________________");

printf("\nEnter operation to perform");

scanf("%d",&ch);

switch(ch)

case 1:

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

for(j=0;j<3;j++)

c[i][j]= a[i][j]+b[i][j];

}printf("\n");

}
printf("Adition ");

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

for(j=0;j<3;j++)

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

}printf("\n");

break;

case 2:

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

for(j=0;j<3;j++)

c[i][j]= a[i][j]*b[i][j];

}printf("\n");

printf("Multiplication ");

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

for(j=0;j<3;j++)

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

}printf("\n");

break;

case 3:
for(i=0;i<3;i++)

for(j=0;j<3;j++)

c[i][j]= a[j][i];

}printf("\n");

printf("Transpose of matrix 1: ");

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

for(j=0;j<3;j++)

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

}printf("\n");

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

for(j=0;j<3;j++)

c[i][j]= b[j][i];

}printf("\n");

printf("Transpose of matrix 2: ");

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

for(j=0;j<3;j++)

printf("%d\t",c[i][j]);
}printf("\n");

break;

default:

printf("Invalid choice");

break;

2a

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct node

int info;

struct node *next;

};

struct node *begin = NULL, *current = NULL, *ptr = NULL, *new = NULL;

void createnode(int size)

int i, data;

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

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

printf("Enter data for node %d : ", i);


scanf("%d", &data);

new->info = data;

new->next = NULL;

if (begin == NULL)

begin = new;

current = new;

else

current->next = new;

current = new;

void display()

ptr = begin;

if (ptr == NULL)

printf("Linked list is empty\n");

else

while (ptr != NULL)

printf("data is: %d\t", ptr->info);


printf("address is: %p\n", ptr->next);

ptr = ptr->next;

void reverse(struct node *temp)

if (temp == NULL)

printf("List is empty\n");

else

if (temp->next == NULL)

printf("data is: %d\n", temp->info);

return;

reverse(temp->next);

printf("data is: %d\n", temp->info);

int main()

int size;

printf("Enter number of nodes: ");

scanf("%d", &size);
createnode(size);

printf("Original linked list:\n");

display();

printf("Reversed linked list:\n");

reverse(begin);

getch();

return 0;

2b

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct node

int info;

struct node *next;

};

struct node *begin, *current = NULL, *new, *ptr;

void createnode(int size)

int i, data;

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

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

printf("Enter data for node %d: ", i+1);


scanf("%d", &data);

new->info = data;

new->next = NULL;

if (begin == NULL)

begin = new;

current = new;

else

current->next = new;

current = new;

void display()

struct node *ptr = begin;

if (ptr == NULL)

printf("List is empty\n");

else

while (ptr != NULL)

{
printf("Data of node: %d\t", ptr->info);

printf("Address: %p\n", ptr->next);

ptr = ptr->next;

void search(struct node *temp)

int se, found = 0;

printf("\nEnter element to be searched: ");

scanf("%d", &se);

while (temp != NULL)

if (temp->info == se)

printf("\nElement found at address: %p\n", temp);

found = 1;

break; // Exit the loop if element is found

else

temp = temp->next;

if (!found)

{
printf("\nElement not found in the list.\n");

int main()

int size;

printf("Enter number of nodes: ");

scanf("%d", &size);

createnode(size);

printf("Linked List:\n");

display();

search(begin);

getch();

return 0;

2c

#include<stdio.h>

#include<stdlib.h>

struct node

int info;

struct node *next;

struct node *prev;

};

struct node *begin, *current = NULL, *new, *ptr;


// Function to create a doubly linked list

void createDoublyLinkedList(int size)

int i, data;

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

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

printf("Enter data for node %d: ", i+1);

scanf("%d", &data);

new->info = data;

new->next = NULL;

new->prev = current;

if (begin == NULL)

begin = new;

current = new;

else

current->next = new;

current = new;

// Function to display the doubly linked list

void displayDoublyLinkedList()
{

struct node *ptr = begin;

if (ptr == NULL)

printf("List is empty\n");

else

while (ptr != NULL)

printf("Data of node: %d\t", ptr->info);

printf("Address: %p\n", ptr);

ptr = ptr->next;

// Function to sort the doubly linked list

void sortDoublyLinkedList()

struct node *temp, *ptr;

int tempData;

if (begin == NULL)

return;

for (ptr = begin; ptr != NULL; ptr = ptr->next)

for (temp = ptr->next; temp != NULL; temp = temp->next)


{

if (ptr->info > temp->info)

tempData = ptr->info;

ptr->info = temp->info;

temp->info = tempData;

int main()

int size;

printf("Enter number of nodes: ");

scanf("%d", &size);

createDoublyLinkedList(size);

printf("Doubly Linked List:\n");

displayDoublyLinkedList();

// Sort the doubly linked list

sortDoublyLinkedList();

printf("Sorted Doubly Linked List:\n");

displayDoublyLinkedList();

return 0;

}
3a

#include<stdio.h>

#include<stdlib.h>

struct node

int info;

struct node *next;

};

struct node *begin, *current = NULL, *new, *ptr;

// Function to create a singly linked list

void createLinkedList(int size)

int i, data;

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

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

printf("Enter data for node %d: ", i+1);

scanf("%d", &data);

new->info = data;

new->next = NULL;

if (begin == NULL)

begin = new;

current = new;

}
else

current->next = new;

current = new;

// Function to display the singly linked list

void displayLinkedList()

struct node *ptr = begin;

if (ptr == NULL)

printf("List is empty\n");

else

while (ptr != NULL)

printf("Data of node: %d\t", ptr->info);

printf("Address: %p\n", ptr);

ptr = ptr->next;

// Function to push (insert at the beginning) an element

void push(int data)


{

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

new->info = data;

new->next = begin;

begin = new;

printf("Pushed %d onto the stack.\n", data);

// Function to pop (delete from the beginning) an element

void pop()

if (begin == NULL)

printf("Stack underflow. The stack is empty.\n");

return;

struct node* temp = begin;

begin = begin->next;

printf("Popped %d from the stack.\n", temp->info);

free(temp);

int main()

int size;

printf("Enter number of nodes: ");

scanf("%d", &size);

createLinkedList(size);

printf("Singly Linked List:\n");


displayLinkedList();

// Push and Pop operations

push(50);

displayLinkedList();

pop();

displayLinkedList();

return 0;

3b

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#define MAX_SIZE 100

// Structure for a node

struct Node {

char data;

struct Node* next;

};

struct Node* head = NULL; // Global variable to store the head of the stack

// Function to push (insert at the beginning) a character onto the stack

void push(char data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;
newNode->next = head;

head = newNode;

// Function to pop (delete from the beginning) a character from the stack

char pop() {

if (head == NULL) {

return '\0';

struct Node* temp = head;

head = head->next;

char poppedData = temp->data;

free(temp);

return poppedData;

// Function to check if a character is an operator

int isOperator(char ch) {

return (ch == '+' || ch == '-' || ch == '*' || ch == '/');

// Function to get the precedence of an operator

int precedence(char ch) {

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

return 1;

if (ch == '*' || ch == '/')

return 2;

return 0;

}
// Function to convert infix expression to postfix expression

void infixToPostfix(char* infix, char* postfix) {

int i = 0, j = 0;

while (infix[i] != '\0') {

if (infix[i] == '(') {

push(infix[i]);

} else if (infix[i] == ')') {

while (head != NULL && head->data != '(') {

postfix[j++] = pop();

pop(); // Pop '(' from the stack

} else if (isOperator(infix[i])) {

while (head != NULL && precedence(infix[i]) <= precedence(head->data)) {

postfix[j++] = pop();

push(infix[i]);

} else {

postfix[j++] = infix[i];

i++;

// Pop remaining operators from the stack

while (head != NULL) {

postfix[j++] = pop();

postfix[j] = '\0'; // Null-terminate the postfix expression

}
// Function to reverse a string

void reverseString(char* str) {

int length = strlen(str);

for (int i = 0; i < length / 2; i++) {

char temp = str[i];

str[i] = str[length - i - 1];

str[length - i - 1] = temp;

// Function to convert infix expression to prefix expression

void infixToPrefix(char* infix, char* prefix) {

// Reverse the infix expression

reverseString(infix);

// Replace '(' with ')' and vice versa

for (int i = 0; infix[i] != '\0'; i++) {

if (infix[i] == '(') {

infix[i] = ')';

} else if (infix[i] == ')') {

infix[i] = '(';

char postfix[MAX_SIZE];

infixToPostfix(infix, postfix);

// Reverse the postfix expression to get prefix expression

reverseString(postfix);
strcpy(prefix, postfix);

int main() {

char infix[MAX_SIZE], postfix[MAX_SIZE], prefix[MAX_SIZE];

printf("Enter infix expression: ");

scanf("%s", infix);

infixToPostfix(infix, postfix);

printf("Postfix expression: %s\n", postfix);

infixToPrefix(infix, prefix);

printf("Prefix expression: %s\n", prefix);

return 0;

3c

#include <stdio.h>

void towerOfHanoi(int n, char source, char auxiliary, char destination) {

if (n == 1) {

printf("Move disk 1 from rod %c to rod %c\n", source, destination);

return;

towerOfHanoi(n - 1, source, destination, auxiliary);

printf("Move disk %d from rod %c to rod %c\n", n, source, destination);

towerOfHanoi(n - 1, auxiliary, source, destination);

}
int main() {

int numDisks;

printf("Enter the number of disks: ");

scanf("%d", &numDisks);

printf("Steps to solve the Tower of Hanoi problem with %d disks:\n", numDisks);

towerOfHanoi(numDisks, 'A', 'B', 'C');

return 0;

4A

#include<stdio.h>

#include<stdlib.h>

struct node

int info;

struct node *next;

};

struct node *front = NULL;

struct node *rear = NULL;

// Function to insert an element into the queue

void insert(int data)

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

newNode->info = data;

newNode->next = NULL;

if (rear == NULL)
{

front = newNode;

rear = newNode;

else

rear->next = newNode;

rear = newNode;

printf("Inserted %d into the queue.\n", data);

// Function to delete an element from the queue

void delete()

if (front == NULL)

printf("Queue is empty. Cannot delete.\n");

return;

struct node* temp = front;

front = front->next;

printf("Deleted %d from the queue.\n", temp->info);

free(temp);

if (front == NULL)

rear = NULL;

}
// Function to display the queue

void display()

struct node *ptr = front;

if (ptr == NULL)

printf("Queue is empty\n");

else

printf("Queue: ");

while (ptr != NULL)

printf("%d ", ptr->info);

ptr = ptr->next;

printf("\n");

int main()

int choice, data;

while (1)

printf("\nQueue Operations:\n");

printf("1. Insert\n");

printf("2. Delete\n");
printf("3. Display\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice)

case 1:

printf("Enter data to insert: ");

scanf("%d", &data);

insert(data);

break;

case 2:

delete();

break;

case 3:

display();

break;

case 4:

printf("Exiting program.\n");

exit(0);

default:

printf("Invalid choice. Please enter a valid choice.\n");

return 0;

}
4b

#include<stdio.h>

#include<stdlib.h>

struct node

int info;

struct node *next;

};

struct node *front = NULL;

struct node *rear = NULL;

// Function to insert an element into the circular queue

void insert(int data)

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

newNode->info = data;

if (rear == NULL)

front = newNode;

rear = newNode;

else

rear->next = newNode;

rear = newNode;

rear->next = front; // Make the circular link


printf("Inserted %d into the queue.\n", data);

// Function to delete an element from the circular queue

void delete()

if (front == NULL)

printf("Queue is empty. Cannot delete.\n");

return;

struct node* temp = front;

printf("Deleted %d from the queue.\n", temp->info);

front = front->next;

free(temp);

// Adjust rear pointer if the queue becomes empty

if (front == NULL)

rear = NULL;

return;

// Adjust rear pointer to the next element if front becomes NULL

rear->next = front;

// Function to display the circular queue

void display()

struct node *ptr = front;


if (ptr == NULL)

printf("Queue is empty\n");

else

printf("Queue: ");

do

printf("%d ", ptr->info);

ptr = ptr->next;

while (ptr != front);

printf("\n");

int main()

int choice, data;

while (1)

printf("\nCircular Queue Operations:\n");

printf("1. Insert\n");

printf("2. Delete\n");

printf("3. Display\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);
switch (choice)

case 1:

printf("Enter data to insert: ");

scanf("%d", &data);

insert(data);

break;

case 2:

delete();

break;

case 3:

display();

break;

case 4:

printf("Exiting program.\n");

exit(0);

default:

printf("Invalid choice. Please enter a valid choice.\n");

return 0;

}
4c

#include<stdio.h>

#include<stdlib.h>

struct node

int info;

struct node *next;

};

struct node *front = NULL;

struct node *rear = NULL;

// Function to insert an element at the front of the deque

void insertFront(int data)

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

newNode->info = data;

newNode->next = front;

front = newNode;

if (rear == NULL)

rear = newNode;

printf("Inserted %d at the front of the deque.\n", data);

// Function to insert an element at the rear of the deque

void insertRear(int data)


{

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

newNode->info = data;

newNode->next = NULL;

if (rear == NULL)

front = newNode;

else

rear->next = newNode;

rear = newNode;

printf("Inserted %d at the rear of the deque.\n", data);

// Function to delete an element from the front of the deque

void deleteFront()

if (front == NULL)

printf("Deque is empty. Cannot delete from front.\n");

return;

struct node *temp = front;

printf("Deleted %d from the front of the deque.\n", temp->info);

front = front->next;

free(temp);

if (front == NULL)
{

rear = NULL;

// Function to delete an element from the rear of the deque

void deleteRear()

if (rear == NULL)

printf("Deque is empty. Cannot delete from rear.\n");

return;

struct node *temp = front;

while (temp->next != rear)

temp = temp->next;

printf("Deleted %d from the rear of the deque.\n", rear->info);

free(rear);

rear = temp;

rear->next = NULL;

if (rear == NULL)

front = NULL;

// Function to display the deque


void display()

struct node *ptr = front;

if (ptr == NULL)

printf("Deque is empty\n");

else

printf("Deque: ");

while (ptr != NULL)

printf("%d ", ptr->info);

ptr = ptr->next;

printf("\n");

int main()

int choice, data;

while (1)

printf("\nDeque Operations:\n");

printf("1. Insert at Front\n");

printf("2. Insert at Rear\n");

printf("3. Delete from Front\n");

printf("4. Delete from Rear\n");


printf("5. Display\n");

printf("6. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice)

case 1:

printf("Enter data to insert at front: ");

scanf("%d", &data);

insertFront(data);

break;

case 2:

printf("Enter data to insert at rear: ");

scanf("%d", &data);

insertRear(data);

break;

case 3:

deleteFront();

break;

case 4:

deleteRear();

break;

case 5:

display();

break;

case 6:

printf("Exiting program.\n");

exit(0);
default:

printf("Invalid choice. Please enter a valid choice.\n");

return 0;

5a

#include <stdio.h>

// Function to perform Bubble Sort

void bubbleSort(int array[], int size) {

int i, j, temp;

for (i = 0; i < size - 1; i++) {

for (j = 0; j < size - i - 1; j++) {

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

// Swap if the elements are in the wrong order

temp = array[j];

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

array[j + 1] = temp;

int main() {

int size, i;

// Input the size of the array

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


scanf("%d", &size);

// Declare an array of given size

int array[size];

// Input elements of the array

printf("Enter %d elements:\n", size);

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

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

// Call Bubble Sort function

bubbleSort(array, size);

// Print the sorted array

printf("Sorted array in ascending order: ");

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

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

printf("\n");

return 0;

5b

#include <stdio.h>

// Function to perform Selection Sort

void selectionSort(int array[], int size) {

int i, j, minIndex, temp;


// Iterate through the array

for (i = 0; i < size - 1; i++) {

// Find the index of the minimum element in the unsorted part of the array

minIndex = i;

for (j = i + 1; j < size; j++) {

if (array[j] < array[minIndex]) {

minIndex = j;

// Swap the minimum element with the first element of the unsorted part

temp = array[minIndex];

array[minIndex] = array[i];

array[i] = temp;

int main() {

int size, i;

// Input the size of the array

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

scanf("%d", &size);

// Declare an array of given size

int array[size];

// Input elements of the array

printf("Enter %d elements:\n", size);

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


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

// Call Selection Sort function

selectionSort(array, size);

// Print the sorted array

printf("Sorted array in ascending order: ");

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

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

printf("\n");

return 0;

5c

#include <stdio.h>

// Function to perform Insertion Sort

void insertionSort(int array[], int size) {

int i, j, key;

// Iterate through the array starting from the second element

for (i = 1; i < size; i++) {

key = array[i];

j = i - 1;

// Move elements of the sorted subarray that are greater than key

// to one position ahead of their current position

while (j >= 0 && array[j] > key) {

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

array[j + 1] = key;

int main() {

int size, i;

// Input the size of the array

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

scanf("%d", &size);

// Declare an array of given size

int array[size];

// Input elements of the array

printf("Enter %d elements:\n", size);

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

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

// Call Insertion Sort function

insertionSort(array, size);

// Print the sorted array

printf("Sorted array in ascending order: ");

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

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


}

printf("\n");

return 0;

6a

#include <stdio.h>

// Function to merge two sorted subarrays

void merge(int array[], int left, int mid, int right) {

int i, j, k;

int n1 = mid - left + 1;

int n2 = right - mid;

// Create temporary arrays

int L[n1], R[n2];

// Copy data to temporary arrays L[] and R[]

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

L[i] = array[left + i];

for (j = 0; j < n2; j++)

R[j] = array[mid + 1 + j];

// Merge the temporary arrays back into array[left..right]

i = 0; // Initial index of first subarray

j = 0; // Initial index of second subarray

k = left; // Initial index of merged subarray

while (i < n1 && j < n2) {

if (L[i] <= R[j]) {

array[k] = L[i];
i++;

} else {

array[k] = R[j];

j++;

k++;

// Copy the remaining elements of L[], if there are any

while (i < n1) {

array[k] = L[i];

i++;

k++;

// Copy the remaining elements of R[], if there are any

while (j < n2) {

array[k] = R[j];

j++;

k++;

// Function to perform Merge Sort

void mergeSort(int array[], int left, int right) {

if (left < right) {

int mid = left + (right - left) / 2;

// Sort first and second halves


mergeSort(array, left, mid);

mergeSort(array, mid + 1, right);

// Merge the sorted halves

merge(array, left, mid, right);

int main() {

int size, i;

// Input the size of the array

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

scanf("%d", &size);

// Declare an array of given size

int array[size];

// Input elements of the array

printf("Enter %d elements:\n", size);

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

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

// Call Merge Sort function

mergeSort(array, 0, size - 1);

// Print the sorted array

printf("Sorted array in ascending order: ");


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

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

printf("\n");

return 0;

6b

#include <stdio.h>

// Function to perform Sequential Search

int sequentialSearch(int array[], int size, int key) {

int i;

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

if (array[i] == key) {

return i; // Return index if key is found

return -1; // Return -1 if key is not found

int main() {

int size, i, key;

// Input the size of the array

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

scanf("%d", &size);

// Declare an array of given size


int array[size];

// Input elements of the array

printf("Enter %d elements:\n", size);

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

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

// Input the key to search

printf("Enter the key to search: ");

scanf("%d", &key);

// Call Sequential Search function

int index = sequentialSearch(array, size, key);

// Print the result

if (index != -1) {

printf("Key found at index: %d\n", index);

} else {

printf("Key not found in the array.\n");

return 0;

6c

#include <stdio.h>

// Function to perform Binary Search

int binarySearch(int array[], int left, int right, int key) {


while (left <= right) {

int mid = left + (right - left) / 2;

// Check if key is present at mid

if (array[mid] == key) {

return mid;

// If key greater, ignore left half

if (array[mid] < key) {

left = mid + 1;

// If key is smaller, ignore right half

else {

right = mid - 1;

// If key is not found, return -1

return -1;

int main() {

int size, i, key;

// Input the size of the array

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

scanf("%d", &size);
// Declare an array of given size (must be sorted for binary search)

int array[size];

// Input elements of the sorted array

printf("Enter %d elements in sorted order:\n", size);

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

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

// Input the key to search

printf("Enter the key to search: ");

scanf("%d", &key);

// Call Binary Search function

int index = binarySearch(array, 0, size - 1, key);

// Print the result

if (index != -1) {

printf("Key found at index: %d\n", index);

} else {

printf("Key not found in the array.\n");

return 0;

You might also like