0% found this document useful (0 votes)
6 views23 pages

Programs List For Internal and External Exam

..

Uploaded by

kadukarsuraj3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
6 views23 pages

Programs List For Internal and External Exam

..

Uploaded by

kadukarsuraj3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 23

Programs list for Internal and External Exam

1. Write a program to perform operation on array(create, insert, delete and display).

#include <stdio.h>

#define MAX 100 // Maximum size of the array

// Function prototypes

void insertElement(int arr[], int *size, int element, int position);

void deleteElement(int arr[], int *size, int position);

void displayArray(int arr[], int size);

int main() {

int array[MAX];

int size = 0; // Current size of the array

int choice, element, position;

while (1) {

printf("\nChoose an operation:\n");

printf("1. Insert element\n");

printf("2. Delete element\n");

printf("3. Display array\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

if (size >= MAX) {

printf("Array is full! Cannot insert more elements.\n");

break;

printf("Enter the element to insert: ");


scanf("%d", &element);

printf("Enter the position (1 to %d): ", size + 1);

scanf("%d", &position);

insertElement(array, &size, element, position - 1); // Convert to 0-based index

break;

case 2:

if (size == 0) {

printf("Array is empty! Nothing to delete.\n");

break;

printf("Enter the position to delete (1 to %d): ", size);

scanf("%d", &position);

deleteElement(array, &size, position - 1); // Convert to 0-based index

break;

case 3:

displayArray(array, size);

break;

case 4:

printf("Exiting program.\n");

return 0;

default:

printf("Invalid choice! Please try again.\n");

// Function to insert an element at a specific position


void insertElement(int arr[], int *size, int element, int position) {

if (position < 0 || position > *size) {

printf("Invalid position! Insertion failed.\n");

return;

for (int i = *size; i > position; i--) {

arr[i] = arr[i - 1];

arr[position] = element;

(*size)++;

printf("Element inserted successfully.\n");

// Function to delete an element at a specific position

void deleteElement(int arr[], int *size, int position) {

if (position < 0 || position >= *size) {

printf("Invalid position! Deletion failed.\n");

return;

for (int i = position; i < *size - 1; i++) {

arr[i] = arr[i + 1];

(*size)--;

printf("Element deleted successfully.\n");

// Function to display the array

void displayArray(int arr[], int size) {

if (size == 0) {

printf("Array is empty.\n");

return;
}

printf("Array contents: ");

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

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

printf("\n");

2. Write a program to search a particular data from the given array of strings using linear search
method.
#include <stdio.h>
#include <string.h>
#define MAX 100

int main() {
char arr[MAX][50], key[50];
int n, i;

printf("Enter number of strings: ");


scanf("%d", &n);
printf("Enter strings:\n");
for (i = 0; i < n; i++) scanf("%s", arr[i]);

printf("Enter string to search: ");


scanf("%s", key);

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


if (strcmp(arr[i], key) == 0) {
printf("Found at position %d.\n", i + 1);
return 0;
}
}
printf("String not found.\n");
return 0;
}
3. Write a program to search a particular data from the given array of numbers using binary
search method
#include <stdio.h>
int binarySearch(int arr[], int n, int key) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key) return mid;
else if (arr[mid] < key) low = mid + 1;
else high = mid - 1;
}
return -1;
}

int main() {
int arr[100], n, key, i;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter sorted elements:\n");
for (i = 0; i < n; i++) scanf("%d", &arr[i]);

printf("Enter number to search: ");


scanf("%d", &key);
int result = binarySearch(arr, n, key);

if (result != -1) printf("Found at position %d.\n", result + 1);


else printf("Not found.\n");
return 0;
}

4. Write a c program to sort an array of numbers using bubble sort

#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 main() {

int arr[100], n, i;

printf("Enter number of elements: ");

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

for (i = 0; i < n; i++) scanf("%d", &arr[i]);

bubbleSort(arr, n);

printf("Sorted array: ");

for (i = 0; i < n; i++) printf("%d ", arr[i]);

return 0;

5. Write a program to sort an array of numbers using Selection sort

in c programming with in short

#include <stdio.h>

void selectionSort(int arr[], int n) {

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

int min_idx = i;

for (int j = i + 1; j < n; j++) {

if (arr[j] < arr[min_idx]) min_idx = j;

int temp = arr[min_idx];

arr[min_idx] = arr[i];

arr[i] = temp;

int main() {

int arr[100], n, i;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter elements:\n");

for (i = 0; i < n; i++) scanf("%d", &arr[i]);


selectionSort(arr, n);

printf("Sorted array: ");

for (i = 0; i < n; i++) printf("%d ", arr[i]);

return 0;

6. Write a program to sort an array of numbers using Insertion sort

#include <stdio.h>

void insertionSort(int arr[], int n) {

for (int i = 1; i < n; i++) {

int key = arr[i], j = i - 1;

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

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

j--;

arr[j + 1] = key;

int main() {

int arr[100], n, i;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter elements:\n");

for (i = 0; i < n; i++) scanf("%d", &arr[i]);

insertionSort(arr, n);

printf("Sorted array: ");

for (i = 0; i < n; i++) printf("%d ", arr[i]);

return 0;
}

7. Write a c program to implement singly linked list with operations 1)insert at beginning
2)Search 3)Display.

#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int data;

struct Node* next;

} Node;

Node* insertAtBeginning(Node* head, int value) {

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

newNode->data = value;

newNode->next = head;

return newNode;

void search(Node* head, int key) {

int pos = 1;

while (head != NULL) {

if (head->data == key) {

printf("Found at position %d.\n", pos);

return;

head = head->next;

pos++;

printf("Not found.\n");

}
void display(Node* head) {

printf("List: ");

while (head != NULL) {

printf("%d -> ", head->data);

head = head->next;

printf("NULL\n");

int main() {

Node* head = NULL;

int choice, value;

while (1) {

printf("\n1. Insert at beginning\n2. Search\n3. Display\n4. Exit\nChoose: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value: ");

scanf("%d", &value);

head = insertAtBeginning(head, value);

break;

case 2:

printf("Enter value to search: ");

scanf("%d", &value);

search(head, value);

break;

case 3:

display(head);

break;
case 4:

return 0;

default:

printf("Invalid choice.\n");

8. Write a c program to add two polynomials using a linked list.

#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int coeff, power;

struct Node* next;

} Node;

Node* createNode(int coeff, int power) {

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

newNode->coeff = coeff;

newNode->power = power;

newNode->next = NULL;

return newNode;

void insert(Node** head, int coeff, int power) {

Node* newNode = createNode(coeff, power);

if (*head == NULL) {

*head = newNode;

return;

Node* temp = *head;


while (temp->next) temp = temp->next;

temp->next = newNode;

Node* addPolynomials(Node* poly1, Node* poly2) {

Node* result = NULL;

while (poly1 && poly2) {

if (poly1->power > poly2->power) {

insert(&result, poly1->coeff, poly1->power);

poly1 = poly1->next;

} else if (poly1->power < poly2->power) {

insert(&result, poly2->coeff, poly2->power);

poly2 = poly2->next;

} else {

insert(&result, poly1->coeff + poly2->coeff, poly1->power);

poly1 = poly1->next;

poly2 = poly2->next;

while (poly1) {

insert(&result, poly1->coeff, poly1->power);

poly1 = poly1->next;

while (poly2) {

insert(&result, poly2->coeff, poly2->power);

poly2 = poly2->next;

return result;

void displayPolynomial(Node* head) {


while (head) {

printf("%dx^%d", head->coeff, head->power);

if (head->next) printf(" + ");

head = head->next;

printf("\n");

int main() {

Node *poly1 = NULL, *poly2 = NULL, *result = NULL;

insert(&poly1, 3, 2); insert(&poly1, 5, 1);

insert(&poly2, 4, 2); insert(&poly2, 2, 0);

result = addPolynomials(poly1, poly2);

printf("Polynomial 1: "); displayPolynomial(poly1);

printf("Polynomial 2: "); displayPolynomial(poly2);

printf("Result: "); displayPolynomial(result);

return 0;

9. Write a program Perform push and pop operations on stack using array

#include <stdio.h>

#define MAX 100

int stack[MAX], top = -1;

void push(int value) {

if (top == MAX - 1) {

printf("Stack overflow.\n");

return;

}
stack[++top] = value;

void pop() {

if (top == -1) {

printf("Stack underflow.\n");

return;

printf("Popped: %d\n", stack[top--]);

void display() {

if (top == -1) {

printf("Stack is empty.\n");

return;

printf("Stack: ");

for (int i = 0; i <= top; i++) printf("%d ", stack[i]);

printf("\n");

int main() {

int choice, value;

while (1) {

printf("\n1. Push\n2. Pop\n3. Display\n4. Exit\nChoose: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value: ");

scanf("%d", &value);
push(value);

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

return 0;

default:

printf("Invalid choice.\n");

10. Write a program Perform push and pop operations on stack using linked list.

#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int data;

struct Node* next;

} Node;

Node* top = NULL;

void push(int value) {

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

if (!newNode) {

printf("Stack overflow.\n");

return;
}

newNode->data = value;

newNode->next = top;

top = newNode;

void pop() {

if (top == NULL) {

printf("Stack underflow.\n");

return;

Node* temp = top;

printf("Popped: %d\n", top->data);

top = top->next;

free(temp);

void display() {

if (top == NULL) {

printf("Stack is empty.\n");

return;

Node* temp = top;

printf("Stack: ");

while (temp) {

printf("%d ", temp->data);

temp = temp->next;

printf("\n");

}
int main() {

int choice, value;

while (1) {

printf("\n1. Push\n2. Pop\n3. Display\n4. Exit\nChoose: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value: ");

scanf("%d", &value);

push(value);

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

return 0;

default:

printf("Invalid choice.\n");

11. Write a c program to perform multiplication of two numbers using recursion.

#include <stdio.h>

int multiply(int a, int b) {

if (b == 0) return 0;
if (b > 0) return a + multiply(a, b - 1);

if (b < 0) return -multiply(a, -b);

int main() {

int a, b;

printf("Enter two numbers: ");

scanf("%d%d", &a, &b);

printf("Result: %d\n", multiply(a, b));

return 0;

12. Write a program Perform insert and delete operations on linear queue using array.

#include <stdio.h>

#define MAX 100

int queue[MAX], front = -1, rear = -1;

void enqueue(int value) {

if (rear == MAX - 1) {

printf("Queue overflow.\n");

return;

if (front == -1) front = 0;

queue[++rear] = value;

void dequeue() {

if (front == -1 || front > rear) {

printf("Queue underflow.\n");

return;

}
printf("Dequeued: %d\n", queue[front++]);

void display() {

if (front == -1 || front > rear) {

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

return;

printf("Queue: ");

for (int i = front; i <= rear; i++) printf("%d ", queue[i]);

printf("\n");

int main() {

int choice, value;

while (1) {

printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\nChoose: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value: ");

scanf("%d", &value);

enqueue(value);

break;

case 2:

dequeue();

break;

case 3:

display();
break;

case 4:

return 0;

default:

printf("Invalid choice.\n");

13. Write a program Perform insert and delete operations on linear queue using linked list.

#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int data;

struct Node* next;

} Node;

Node *front = NULL, *rear = NULL;

void enqueue(int value) {

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

if (!newNode) {

printf("Queue overflow.\n");

return;

newNode->data = value;

newNode->next = NULL;

if (rear == NULL) {

front = rear = newNode;

return;

}
rear->next = newNode;

rear = newNode;

void dequeue() {

if (front == NULL) {

printf("Queue underflow.\n");

return;

Node* temp = front;

printf("Dequeued: %d\n", front->data);

front = front->next;

if (front == NULL) rear = NULL;

free(temp);

void display() {

if (front == NULL) {

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

return;

printf("Queue: ");

Node* temp = front;

while (temp) {

printf("%d ", temp->data);

temp = temp->next;

printf("\n");

int main() {
int choice, value;

while (1) {

printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\nChoose: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value: ");

scanf("%d", &value);

enqueue(value);

break;

case 2:

dequeue();

break;

case 3:

display();

break;

case 4:

return 0;

default:

printf("Invalid choice.\n");

14. Write a c program to perform insert and delete operations on circular queue using an
array.

#include <stdio.h>

#define MAX 100

int queue[MAX], front = -1, rear = -1;


void enqueue(int value) {

if ((rear + 1) % MAX == front) {

printf("Queue overflow.\n");

return;

if (front == -1) front = 0;

rear = (rear + 1) % MAX;

queue[rear] = value;

void dequeue() {

if (front == -1) {

printf("Queue underflow.\n");

return;

printf("Dequeued: %d\n", queue[front]);

if (front == rear) front = rear = -1;

else front = (front + 1) % MAX;

void display() {

if (front == -1) {

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

return;

printf("Queue: ");

for (int i = front; i != rear; i = (i + 1) % MAX) printf("%d ", queue[i]);

printf("%d\n", queue[rear]);

}
int main() {

int choice, value;

while (1) {

printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\nChoose: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value: ");

scanf("%d", &value);

enqueue(value);

break;

case 2:

dequeue();

break;

case 3:

display();

break;

case 4:

return 0;

default:

printf("Invalid choice.\n");

You might also like