Final
Final
#include <stdio.h>
int main() {
int num1, num2, num3, max;
max = num1;
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;
}
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);
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++;
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;
size--;
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);
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>
int main() {
int n, target;
int arr[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
bubbleSort(arr, n);
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);
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;
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;
}
}
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;
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++;
}
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;
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++;
}
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...
#include <stdio.h>
#define MAX 10
int queue[MAX];
int front = -1, rear = -1;
int main() {
int choice, value, k;
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");
}
}
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};
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