2.Find the maximum of three numbers.
#include <stdio.h>
int main() {
int num1, num2, num3, max;
scanf("%d %d %d", &num1, &num2, &num3);
max = num1;
if (num2 > max) max = num2;
if (num3 > max) max = num3;
printf("%d\n", max);
return 0;
}
OUTPUT:
3 7 9
9
3.Check whether a number is prime or not.
#include <stdio.h>
int main() {
int num, i;
scanf("%d", &num);
if (num <= 1) {
printf("%d is not a prime number.\n", num);
return 0;
}
for (i = 2; i * i <= num; i++) {
if (num % i == 0) {
printf("%d is not a prime number.\n", num);
return 0;
}
}
printf("%d is a prime number.\n", num);
return 0;
}
OUTPUT:
5
5 is a prime number.
4.Calculate the factorial of a number.
#include <stdio.h>
int main() {
int i, num, fact = 1;
scanf("%d", &num);
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
for (i = 1; i <= num; i++) {
fact = fact * i;
}
printf("%d\n", fact);
}
return 0;
}
OUTPUT:
5
120
5.Print Fibonacci series up to a given number.
#include <stdio.h>
int main() {
int n, i, first = 0, second = 1, next;
scanf("%d", &n);
for (i = 0; i < n; i++) {
if (i <= 1) {
next = i;
} else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}
printf("\n");
return 0;
}
OUTPUT:
5
0 1 1 2 3
1.Write a program to insert an element into the given array at a certain position,
then print the whole array.
#include <stdio.h>
int main() {
int Arr[20] = {57, 88, 12, 97, 34, 61, 45, 23, 55, 70, 39, 46, 94};
int size = 13;
int insert, position, i;
printf("Enter the value to insert: ");
scanf("%d", &insert);
do {
printf("Enter the position to insert at (0-%d index): ", size);
scanf("%d", &position);
if (position < 0 || position > size) {
printf("Invalid position! Please enter a position between 0 and %d.\n",
size);
}
} while (position < 0 || position > size);
for (i = size; i > position; i--) {
Arr[i] = Arr[i - 1];
}
Arr[position] = insert;
size++;
printf("Updated array: ");
for (i = 0; i < size; i++) {
printf("%d ", Arr[i]);
}
printf("\n");
return 0;
}
OUTPUT:
Enter the value to insert: 5
Enter the position to insert at (0-based index): 2
Updated array: 57 88 5 12 97 34 61 45 23 55 70 39 46 94
2. Write a program to delete an element from the given array from a certain
position,then print the whole array.
#include <stdio.h>
int main() {
int Arr[20] = {57, 88, 12, 97, 34, 61, 45, 23, 55, 70, 39, 46, 94};
int size = 13;
int position, i;
printf("Enter the position to delete (0-based index): ");
scanf("%d", &position);
for (i = position; i < size - 1; i++) {
Arr[i] = Arr[i + 1];
}
size--;
printf("Updated array: ");
for (i = 0; i < size; i++) {
printf("%d ", Arr[i]);
}
printf("\n");
return 0;
}
OUTPUT:
Enter the position to delete (0-based index): 2
Updated array: 57 88 97 34 61 45 23 55 70 39 46 94
3.write a program to find the position of a certain element in the given array
using binary search algorithm.
#include <stdio.h>
int main() {
int Arr[20] = {57, 88, 12, 97, 34, 61, 45, 23, 55, 70, 39, 46, 94};
int size = 13;
int find, position = -1, i;
printf("Enter the value to search for: ");
scanf("%d", &find);
for (i = 0; i < size; i++) {
if (Arr[i] == find) {
position = i;
break;
}
}
if (position != -1) {
printf("Value %d found at position %d\n", find, position);
} else {
printf("Value %d not found in the array.\n", find);
}
return 0;
}
OUTPUT:
Enter the value to search for: 4
Value 4 not found in the array.
To write a program to find a number from an array using bubble sort and binary
search algorithm.
#include <stdio.h>
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int binarySearch(int arr[], int n, int target) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
int main() {
int n, target;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
bubbleSort(arr, n);
printf("Sorted Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
printf("Enter the number to search: ");
scanf("%d", &target);
int result = binarySearch(arr, n, target);
if (result != -1) {
printf("Number found at index %d\n", result);
} else {
printf("Number not found\n");
}
return 0;
}
OUTPUT:
Enter the number of elements: 4
Enter the elements: 7 5 2 9
Sorted Array: 2 5 7 9
Enter the number to search: 7
Number found at index 2
1.
#include <stdio.h>
int main() {
int DATA[10] = {45, 57, 98, 12, 76, 0, 10, 23, 9, 0};
int LINK[10] = {7, 10, 2, -1, 1, -1, 3, 4, 8, 6};
int start = 5;
int K;
scanf("%d", &K);
int current = start;
DATA[current - 1] += K;
while (current != -1) {
printf("DATA: %d, LINK: %d\n", DATA[current - 1], LINK[current - 1]);
current = LINK[current - 1];
}
return 0;
}
OUTPUT:
100
DATA: 176, LINK: 1
DATA: 45, LINK: 7
DATA: 10, LINK: 3
DATA: 98, LINK: 2
DATA: 57, LINK: 10
DATA: 0, LINK: 6
DATA: 0, LINK: -1
2.
#include <stdio.h>
int main() {
int data[100] = {45, 57, 98, 12, 76, 0, 10, 23, 9, 0};
int link[100] = {7, 10, 2, -1, 1, -1, 3, 4, 8, 6};
int start = 5, avail = 9, pos, newData, ptr, newIndex;
printf("Original Linked List: ");
for (ptr = start; ptr != -1; ptr = link[ptr - 1]) {
printf("%d ", data[ptr - 1]);
}
printf("\n");
scanf("%d%d", &pos, &newData);
if (avail == -1) return printf("No space.\n"), 0;
newIndex = avail;
avail = link[avail - 1];
data[newIndex - 1] = newData;
if (pos == -1) {
link[newIndex - 1] = start;
start = newIndex;
} else {
int current = start, prev = -1, currentPos = 1;
while (current != -1 && currentPos < pos) {
prev = current;
current = link[current - 1];
currentPos++;
}
if (currentPos == pos) {
link[newIndex - 1] = current;
if (prev != -1) {
link[prev - 1] = newIndex;
}
} else {
return printf("Invalid position.\n"), 0;
}
}
printf("Updated Linked List: ");
for (ptr = start; ptr != -1; ptr = link[ptr - 1]) {
printf("%d ", data[ptr - 1]);
}
printf("\n");
return 0;
}
OUTPUT:
Original Linked List: 76 45 10 98 57 0 0
3
100
Updated Linked List: 76 45 100 10 98 57 0 0
3.
#include <stdio.h>
int main() {
int data[100] = {45, 57, 98, 12, 76, 0, 10, 23, 9, 0};
int link[100] = {7, 10, 2, -1, 1, -1, 3, 4, 8, 6};
int start = 5, avail = 9, pos, ptr;
printf("Original Linked List: ");
for (ptr = start; ptr != -1; ptr = link[ptr - 1]) {
printf("%d ", data[ptr - 1]);
}
printf("\n");
scanf("%d", &pos);
if (pos == -1) {
printf("Invalid position.\n");
return 0;
}
if (start == -1) {
printf("The list is empty.\n");
return 0;
}
if (pos == 1) {
int temp = start;
start = link[start - 1];
link[temp - 1] = avail;
avail = temp;
} else {
int current = start, prev = -1, currentPos = 1;
while (current != -1 && currentPos < pos) {
prev = current;
current = link[current - 1];
currentPos++;
}
if (current != -1 && currentPos == pos) {
link[prev - 1] = link[current - 1];
link[current - 1] = avail;
avail = current;
} else {
printf("Invalid position.\n");
return 0;
}
}
printf("Updated Linked List: ");
for (ptr = start; ptr != -1; ptr = link[ptr - 1]) {
printf("%d ", data[ptr - 1]);
}
printf("\n");
return 0;
}
OUTPUT:
Original Linked List: 76 45 10 98 57 0 0
4
Updated Linked List: 76 45 10 57 0 0
Question :
Write a program to implement a stack using an array. Your program
should provide the following operations:
1. Push: Add a new element to the top of the stack. Display an error
message if the stack is full.
2. Pop: Remove and display the top element of the stack. Display an
error message if the stack is empty.
3. Show: Display all the elements currently in the stack from top to
bottom.
#include <stdio.h>
#define MAX 10
int main() {
char stack[MAX];
int top = -1;
int choice;
char value;
int maxLoops = 5;
int loopCount = 0;
while (loopCount < maxLoops) {
printf("\nStack Operations:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Show\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 1) {
if (top == MAX - 1) {
printf("Stack Overflow!\n");
} else {
printf("Enter value to push (single character): ");
scanf(" %c", &value);
stack[++top] = value;
}
} else if (choice == 2) {
if (top == -1) {
printf("Stack Underflow!\n");
} else {
value = stack[top--];
printf("Popped value: %c\n", value);
}
} else if (choice == 3) {
if (top == -1) {
printf("Stack is empty!\n");
} else {
printf("Top value: %c\n", stack[top]);
}
} else if (choice == 4) {
if (top == -1) {
printf("Stack is empty!\n");
} else {
printf("Stack contents: ");
for (int i = 0; i <= top; i++) {
printf("%c ", stack[i]);
}
printf("\n");
}
} else if (choice == 5) {
break;
} else {
printf("Invalid choice!\n");
}
loopCount++;
}
printf("Maximum operations reached. Exiting...\n");
return 0;
}
OUTPUT:
Stack Operations:
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 1
Enter value to push (single character): A
Stack Operations:
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 1
Enter value to push (single character): B
Stack Operations:
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 4
Stack contents: A B
Stack Operations:
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 2
Popped value: B
Stack Operations:
1. Push
2. Pop
3. Peek
4. Show
5. Exit
Enter your choice: 4
Stack contents: A
Maximum operations reached. Exiting...
Question : Write a program to implement queue data structure.
User option :
1. Enqueue
2.Dequeue
3.Display
4.add value k to every node
#include <stdio.h>
#define MAX 10
int queue[MAX];
int front = -1, rear = -1;
int main() {
int choice, value, k;
for (int i = 0; i < 5; i++) {
printf("\nQueue Operations:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Add value k to every node\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
if (rear == MAX - 1) {
printf("Queue is full\n");
} else {
printf("Enter value to enqueue: ");
scanf("%d", &value);
if (front == -1) {
front = 0;
}
rear++;
queue[rear] = value;
printf("Enqueued %d\n", value);
}
break;
case 2:
if (front == -1 || front > rear) {
printf("Queue is empty\n");
} else {
printf("Dequeued %d\n", queue[front]);
front++;
}
break;
case 3:
if (front == -1 || front > rear) {
printf("Queue is empty\n");
} else {
printf("Queue: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
break;
case 4:
printf("Enter value k to add to each element: ");
scanf("%d", &k);
if (front == -1 || front > rear) {
printf("Queue is empty\n");
} else {
for (int i = front; i <= rear; i++) {
queue[i] += k;
}
printf("Added %d to every element in the queue\n", k);
}
break;
case 5:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
printf("Maximum operations reached. Exiting program.\n");
return 0;
}
OUTPUT:
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Add value k to every node
5. Exit
Enter your choice: 1
Enter value to enqueue: 10
Enqueued 10
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Add value k to every node
5. Exit
Enter your choice: 1
Enter value to enqueue: 20
Enqueued 20
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Add value k to every node
5. Exit
Enter your choice: 3
Queue: 10 20
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Add value k to every node
5. Exit
Enter your choice: 4
Enter value k to add to each element: 5
Added 5 to every element in the queue
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Add value k to every node
5. Exit
Enter your choice: 3
Queue: 15 25
Maximum operations reached. Exiting program.
#include <stdio.h>
#define SIZE 8
int values[SIZE] = {50, 30, 70, 20, 40, 60, 70, 10};
int left[SIZE] = {1, 3, 5, 7, -1, -1, -1, -1};
int right[SIZE] = {2, 4, 6, -1, -1, -1, -1, -1};
void preorder(int index) {
if (index == -1) return;
printf("%d ", values[index]);
preorder(left[index]);
preorder(right[index]);
}
void inorder(int index) {
if (index == -1) return;
inorder(left[index]);
printf("%d ", values[index]);
inorder(right[index]);
}
void postorder(int index) {
if (index == -1) return;
postorder(left[index]);
postorder(right[index]);
printf("%d ", values[index]);
}
int main() {
printf("Preorder Traversal: ");
preorder(0);
printf("\nInorder Traversal: ");
inorder(0);
printf("\nPostorder Traversal: ");
postorder(0);
printf("\n");
return 0;
}
OUTPUT:
Preorder Traversal: 50 30 20 10 40 70 60 70
Inorder Traversal: 10 20 30 40 50 60 70 70
Postorder Traversal: 10 20 40 30 60 70 70 50