💻 1. Implementation of insertion at a specific position in an array using function and display it.
#include <stdio.h>
#define MAX_SIZE 100
void insertAtPosition(int arr[], int *size, int element, int position)
{
if (*size >= MAX_SIZE)
{
printf("Error: Array is full.\n");
return;
}
if (position < 1 || position > *size + 1)
{
printf("Error: Invalid position.\n");
return;
}
for (int i = *size; i >= position; i--)
{
arr[i] = arr[i - 1];
}
arr[position - 1] = element;
(*size)++;
printf("Inserted %d at position %d.\n", element, position);
}
void displayArray(int arr[], int size)
{
if (size == 0)
{
printf("Array is empty.\n");
return;
}
printf("Array elements: ");
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int arr[MAX_SIZE];
int size = 0;
int choice, element, position;
while (1)
{
printf("\nMenu:\n");
printf("1. Insert element\n");
printf("2. Display array\n");
printf("3. Exit\n");
printf("Enter your choice: ");
if (scanf("%d", &choice) != 1)
{
printf("Invalid input. Exiting.\n");
return 0;
}
switch (choice)
{
case 1:
printf("Enter element: ");
scanf("%d", &element);
printf("Enter position (1 to %d): ", size + 1);
scanf("%d", &position);
insertAtPosition(arr, &size, element, position);
break;
case 2:
displayArray(arr, size);
break;
case 3:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice.\n");
}
}
}
💻 2. Implementation of deletion at a specific position in an array using a function and display it.
#include <stdio.h>
#define MAX_SIZE 10
void deleteAtPosition(int arr[], int *size, int position)
{
if (*size == 0)
{
printf("Error: Array is empty.\n");
return;
}
if (position < 1 || position > *size)
{
printf("Error: Invalid position.\n");
return;
}
int deleted = arr[position - 1];
for (int i = position - 1; i < *size - 1; i++)
{
arr[i] = arr[i + 1];
}
(*size)--;
printf("Deleted element: %d\n", deleted);
}
void displayArray(int arr[], int size)
{
if (size == 0)
{
printf("Array is empty.\n");
return;
}
printf("Array elements: ");
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int arr[MAX_SIZE] = {10,20,30,40,50,60,70,80,90,100};
int size = 10;
int choice, position;
while (1)
{
printf("\nMenu:\n");
printf("1. Delete element\n");
printf("2. Display array\n");
printf("3. Exit\n");
printf("Enter your choice: ");
if (scanf("%d", &choice) != 1)
{
printf("Invalid input. Exiting.\n");
return 0;
}
switch (choice)
{
case 1:
printf("Enter position (1 to %d): ", size);
scanf("%d", &position);
deleteAtPosition(arr, &size, position);
break;
case 2:
displayArray(arr, size);
break;
case 3:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice.\n");
}
}
}
💻 3. Write a program to calculate factorial of any number using recursion.
#include <stdio.h>
long long factorial(int n)
{
if (n < 0)
{
return -1; // error case
}
if (n == 0 || n == 1)
{
return 1;
}
return n * factorial(n - 1);
}
int main()
{
int num;
printf("Enter a number: ");
if (scanf("%d", &num) != 1)
{
printf("Invalid input.\n");
return 0;
}
long long result = factorial(num);
if (result == -1)
{
printf("Factorial not defined for negative numbers.\n");
}
else
{
printf("Factorial of %d is: %lld\n", num, result);
}
return 0;
}
💻 4. To implement push and display on a stack using array.
#include <stdio.h>
#define SIZE 5
int top = -1;
int STK[SIZE];
int isfull()
{
return (top == SIZE - 1);
}
int isempty()
{
return (top == -1);
}
void push(int val)
{
if (isfull())
{
printf("----- Stack is FULL -----\n");
}
else
{
top++;
STK[top] = val;
printf("Pushed %d into stack.\n", val);
}
}
void display()
{
if (isempty())
{
printf("----- Stack is EMPTY -----\n");
}
else
{
printf("Stack contents (top to bottom):\n");
for (int i = top; i >= 0; i--)
{
printf("%d\n", STK[i]);
}
}
}
int main()
{
int choice, val;
do
{
printf("\n---------------------------------\n");
printf("1. Push\n2. Display\n3. Exit\n");
printf("---------------------------------\n");
printf("Enter your choice: ");
if (scanf("%d", &choice) != 1)
{
printf("Invalid input.\n");
return 0;
}
switch (choice)
{
case 1:
printf("Enter value: ");
scanf("%d", &val);
push(val);
break;
case 2:
display();
break;
case 3:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice.\n");
}
} while (choice != 3);
return 0;
}
💻 5. To implement pop and display on a stack using array.
#include <stdio.h>
#define SIZE 5
int top = 4;
int STK[SIZE] = {10, 20, 30, 50, 40};
int isempty()
{
return (top == -1);
}
int pop()
{
if (isempty())
{
printf("----- Stack is EMPTY -----\n");
return -1;
}
else
{
int val = STK[top];
top--;
return val;
}
}
void display()
{
if (isempty())
{
printf("----- Stack is EMPTY -----\n");
}
else
{
printf("Stack contents (top to bottom):\n");
for (int i = top; i >= 0; i--)
{
printf("%d\n", STK[i]);
}
}
}
int main()
{
int choice, val;
do
{
printf("\n---------------------------------\n");
printf("1. Pop\n2. Display\n3. Exit\n");
printf("---------------------------------\n");
printf("Enter your choice: ");
if (scanf("%d", &choice) != 1)
{
printf("Invalid input.\n");
return 0;
}
switch (choice)
{
case 1:
val = pop();
if (val != -1)
printf("Popped value = %d\n", val);
break;
case 2:
display();
break;
case 3:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice.\n");
}
} while (choice != 3);
return 0;
}
💻 6. Implement Enqueue using array implementation of Linear Queue
#include <stdio.h>
#define SIZE 5
int rear = -1;
int front = 0;
int que[SIZE];
int isquefull()
{
return (rear == SIZE - 1);
}
int isqueempty()
{
return (front > rear);
}
void enqueue(int val)
{
if (isquefull())
{
printf("----- Queue is FULL -----\n");
}
else
{
rear++;
que[rear] = val;
printf("Inserted %d into queue.\n", val);
}
}
void display()
{
if (isqueempty())
{
printf("----- Queue is EMPTY -----\n");
}
else
{
printf("Queue contents:\n");
for (int i = front; i <= rear; i++)
{
printf("%d\n", que[i]);
}
}
}
int main()
{
int choice, val;
do
{
printf("\n-------------------------\n");
printf("1. Enqueue\n2. Display\n3. Exit\n");
printf("-------------------------\n");
printf("Enter your choice: ");
if (scanf("%d", &choice) != 1)
{
printf("Invalid input.\n");
return 0;
}
switch (choice)
{
case 1:
printf("Enter value: ");
scanf("%d", &val);
enqueue(val);
break;
case 2:
display();
break;
case 3:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice.\n");
}
} while (choice != 3);
return 0;
}
💻 7. Implement Dequeue using array implementation of Linear Queue
#include <stdio.h>
#define SIZE 5
int rear = 4;
int front = 0;
int que[SIZE] = {100, 200, 300, 400, 500};
int isqueempty()
{
return (front > rear);
}
int dequeue()
{
if (isqueempty())
{
printf("----- Queue is EMPTY -----\n");
return -1;
}
else
{
int val = que[front];
front++;
return val;
}
}
void display()
{
if (isqueempty())
{
printf("----- Queue is EMPTY -----\n");
}
else
{
printf("Queue contents:\n");
for (int i = front; i <= rear; i++)
{
printf("%d\n", que[i]);
}
}
}
int main()
{
int choice, val;
do
{
printf("\n-------------------------\n");
printf("1. Dequeue\n2. Display\n3. Exit\n");
printf("-------------------------\n");
printf("Enter your choice: ");
if (scanf("%d", &choice) != 1)
{
printf("Invalid input.\n");
return 0;
}
switch (choice)
{
case 1:
val = dequeue();
if (val != -1)
printf("Dequeued value = %d\n", val);
break;
case 2:
display();
break;
case 3:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice.\n");
}
} while (choice != 3);
return 0;
}
💻 8. To implement enqueue on a circular queue using array
#include <stdio.h>
#define MAX 6
int queue[MAX];
int front = -1;
int rear = -1;
void enqueue(int element)
{
if ((rear + 1) % MAX == front)
{
printf("Queue is FULL\n");
}
else if (front == -1 && rear == -1)
{
front = rear = 0;
queue[rear] = element;
}
else
{
rear = (rear + 1) % MAX;
queue[rear] = element;
}
}
void display()
{
if (front == -1 && rear == -1)
{
printf("Queue is EMPTY\n");
}
else
{
int i = front;
printf("Queue elements:\n");
while (1)
{
printf("%d\n", queue[i]);
if (i == rear)
break;
i = (i + 1) % MAX;
}
}
}
int main()
{
int choice, x;
do
{
printf("\n1. Enqueue\n2. Display\n3. Exit\n");
printf("Enter choice: ");
if (scanf("%d", &choice) != 1)
return 0;
switch (choice)
{
case 1:
printf("Enter element: ");
scanf("%d", &x);
enqueue(x);
break;
case 2:
display();
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 3);
return 0;
}
💻 9. To implement dequeue on a circular queue using array
#include <stdio.h>
#define MAX 6
int queue[MAX] = {100, 200, 300, 400, 500};
int front = 0;
int rear = 4;
int dequeue()
{
if (front == -1 && rear == -1)
{
printf("Queue is EMPTY\n");
return -1;
}
else if (front == rear)
{
int val = queue[front];
front = -1;
rear = -1;
return val;
}
else
{
int val = queue[front];
front = (front + 1) % MAX;
return val;
}
}
void display()
{
if (front == -1 && rear == -1)
{
printf("Queue is EMPTY\n");
}
else
{
int i = front;
printf("Queue elements:\n");
while (1)
{
printf("%d\n", queue[i]);
if (i == rear)
break;
i = (i + 1) % MAX;
}
}
}
int main()
{
int choice, val;
do
{
printf("\n1. Dequeue\n2. Display\n3. Exit\n");
printf("Enter choice: ");
if (scanf("%d", &choice) != 1)
return 0;
switch (choice)
{
case 1:
val = dequeue();
if (val != -1)
printf("Dequeued element = %d\n", val);
break;
case 2:
display();
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 3);
return 0;
}