Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 56

ARRAYS

**Problem 1:**

Write a C program to find the sum of all elements in an integer array.

#include <stdio.h>

int main() {

int arr[] = {1, 2, 3, 4, 5};

int n = sizeof(arr) / sizeof(arr[0]);

int sum = 0;

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

sum += arr[i];

printf("Sum of array elements: %d\n", sum);

return 0;

**Problem 2:**

Write a C program to find the largest and smallest elements in an array.

#include <stdio.h>

void findMinMax(int arr[], int size, int *min, int *max) {

*min = *max = arr[0];

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

if (arr[i] < *min)

*min = arr[i];

if (arr[i] > *max)

*max = arr[i];

int main() {
int arr[] = {12, 5, 7, 19, 22, 8};

int size = sizeof(arr) / sizeof(arr[0]);

int min, max;

findMinMax(arr, size, &min, &max);

printf("Smallest element: %d\n", min);

printf("Largest element: %d\n", max);

return 0;

```

**Problem 3:**

Write a C program to implement a matrix multiplication function that takes two matrices as input and returns the result. Ensure proper error
handling for incompatible matrix sizes.

```c

#include <stdio.h>

void multiplyMatrices(int A[][3], int B[][2], int result[][2], int m, int n, int p) {

if (n != p) {

printf("Matrix multiplication not possible. Incompatible sizes.\n");

return;

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

for (int j = 0; j < p; j++) {

result[i][j] = 0;

for (int k = 0; k < n; k++) {

result[i][j] += A[i][k] * B[k][j];

int main() {

int A[][3] = {{2, 3, 4}, {5, 6, 7}};


int B[][2] = {{8, 9}, {10, 11}, {12, 13}};

int result[2][2];

multiplyMatrices(A, B, result, 2, 3, 2);

// Display the result matrix

printf("Resultant Matrix:\n");

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

for (int j = 0; j < 2; j++) {

printf("%d ", result[i][j]);

printf("\n");

return 0;

**Problem 4:**

Write a C program that finds the second largest element in an array of integers.

```c

#include <stdio.h>

int findSecondLargest(int arr[], int size) {

int firstMax, secondMax;

if (arr[0] > arr[1]) {

firstMax = arr[0];

secondMax = arr[1];

} else {

firstMax = arr[1];

secondMax = arr[0];

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

if (arr[i] > firstMax) {

secondMax = firstMax;

firstMax = arr[i];
} else if (arr[i] > secondMax && arr[i] != firstMax) {

secondMax = arr[i];

return secondMax;

int main() {

int size;

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

scanf("%d", &size);

int array[size];

printf("Enter elements for the array:\n");

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

printf("Element %d: ", i + 1);

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

int result = findSecondLargest(array, size);

printf("Second largest element in the array: %d\n", result);

return 0;

```

**Problem 5:**

Write a C program that takes an array of integers as input and finds the sum and product of the elements.

**Solution:**

```c

#include <stdio.h>

int main() {

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

scanf("%d", &size);

int array[size];

printf("Enter elements for the array:\n");

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

printf("Element %d: ", i + 1);

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

int sum = 0, product = 1;

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

sum += array[i];

product *= array[i];

printf("Sum of the array elements: %d\n", sum);

printf("Product of the array elements: %d\n", product);

return 0;

}
POINTERS
**Problem 1:**

Write a C program to swap the values of two integers using pointers.

```c

#include <stdio.h>

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

int main() {

int x = 5, y = 10;

printf("Before swapping: x = %d, y = %d\n", x, y);

swap(&x, &y);

printf("After swapping: x = %d, y = %d\n", x, y);

return 0;

**Problem 2:**

Write a C program to find the length of a string using pointers.

```c

#include <stdio.h>

int stringLength(const char *str) {

const char *ptr = str;

while (*ptr != '\0') {

ptr++;
}

return ptr - str;

int main() {

const char *message = "Hello, World!";

printf("Length of the string: %d\n", stringLength(message));

return 0;

**Problem 3:**

Write a C program to implement a function that receives an array of integers and a function pointer. The function should use the function pointer
to perform a specific operation (e.g., square, cube, etc.) on each element of the array and print the modified array.

```c

#include <stdio.h>

void performOperation(int arr[], int size, int (*operation)(int)) {

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

arr[i] = operation(arr[i]);

int square(int x) {

return x * x;

int cube(int x) {

return x * x * x;

int main() {

int arr[] = {1, 2, 3, 4, 5};

int size = sizeof(arr) / sizeof(arr[0]);

printf("Original Array:\n");
for (int i = 0; i < size; i++) {

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

performOperation(arr, size, square);

printf("\nArray after squaring:\n");

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

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

performOperation(arr, size, cube);

printf("\nArray after cubing:\n");

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

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

return 0;

```

**Problem 4:**

Write a C program to swap the values of two integers using pointers.

```c

#include <stdio.h>

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

int main() {

int num1, num2;


printf("Enter two integers:\n");

printf("Number 1: ");

scanf("%d", &num1);

printf("Number 2: ");

scanf("%d", &num2);

printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);

swap(&num1, &num2);

printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);

return 0;

```

**Problem 5:**

Write a C program that takes an integer input, calculates its square and cube using pointers, and prints the results.

```c

#include <stdio.h>

void calculateSquareAndCube(int num, int *square, int *cube) {

*square = num * num;

*cube = num * num * num;

int main() {

int num, square, cube;

printf("Enter an integer: ");

scanf("%d", &num);

calculateSquareAndCube(num, &square, &cube);

printf("Square of %d: %d\n", num, square);


printf("Cube of %d: %d\n", num, cube);

return 0;

```

QUEUES
**Problem 1:**

Implement a simple queue in C using an array. Include functions for enqueue and dequeue operations.

```c

#include <stdio.h>

#define MAX_SIZE 5

int queue[MAX_SIZE];

int front = -1, rear = -1;

void enqueue(int item) {

if (rear == MAX_SIZE - 1) {

printf("Queue is full. Cannot enqueue.\n");

return;

if (front == -1)

front = 0;

queue[++rear] = item;

printf("%d enqueued to the queue\n", item);

int dequeue() {

if (front == -1) {

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

return -1;

int item = queue[front++];


printf("%d dequeued from the queue\n", item);

if (front > rear)

front = rear = -1;

return item;

int main() {

enqueue(10);

enqueue(20);

enqueue(30);

dequeue();

dequeue();

dequeue();

return 0;

```

**Problem 2:**

Implement a circular queue in C using an array. Include functions for enqueue and dequeue operations.

```c

#include <stdio.h>

#define MAX_SIZE 5

int queue[MAX_SIZE];

int front = -1, rear = -1;

void enqueue(int item) {

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

printf("Queue is full. Cannot enqueue.\n");

return;

if (front == -1)

front = 0;
rear = (rear + 1) % MAX_SIZE;

queue[rear] = item;

printf("%d enqueued to the queue\n", item);

int dequeue() {

if (front == -1) {

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

return -1;

int item = queue[front];

if (front == rear)

front = rear = -1;

else

front = (front + 1) % MAX_SIZE;

printf("%d dequeued from the queue\n", item);

return item;

int main() {

enqueue(10);

enqueue(20);

enqueue(30);

dequeue();

dequeue();

dequeue();

return 0;

```

**Problem 3:**

Implement a priority queue in C using a linked list. Include functions for enqueue with priority and dequeue operations.
```c

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

int priority;

struct Node *next;

};

struct Node *front = NULL;

void enqueueWithPriority(int item, int priority) {

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

if (newNode == NULL) {

printf("Memory allocation failed. Cannot enqueue.\n");

return;

newNode->data = item;

newNode->priority = priority;

if (front == NULL || priority < front->priority) {

newNode->next = front;

front = newNode;

} else {

struct Node *temp = front;

while (temp->next != NULL && temp->next->priority <= priority) {

temp = temp->next;

newNode->next = temp->next;

temp->next = newNode;

printf("%d enqueued to the priority queue with priority %d\n", item, priority);

int dequeue() {
if (front == NULL) {

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

return -1;

struct Node *temp = front;

int item = temp->data;

front = front->next;

free(temp);

printf("%d dequeued from the priority queue\n", item);

return item;

int main() {

enqueueWithPriority(10, 2);

enqueueWithPriority(20, 1);

enqueueWithPriority(30, 3);

dequeue();

dequeue();

dequeue();

return 0;

```

STACKS
**Problem 1:**

Implement a stack in C using an array. Include functions for push and pop operations.

```c

#include <stdio.h>

#define MAX_SIZE 5

int stack[MAX_SIZE];

int top = -1;

void push(int item) {

if (top == MAX_SIZE - 1) {

printf("Stack is full. Cannot push.\n");

return;

stack[++top] = item;

printf("%d pushed to the stack\n", item);

int pop() {

if (top == -1) {

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

return -1;

int item = stack[top--];

printf("%d popped from the stack\n", item);

return item;

int main() {

push(10);

push(20);

push(30);

pop();
pop();

pop();

return 0;

```

**Problem 2**

Implement a stack in C using linked lists. Include functions for push and pop operations.

```c

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node *next;

};

struct Node *top = NULL;

void push(int item) {

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

if (newNode == NULL) {

printf("Memory allocation failed. Cannot push.\n");

return;

newNode->data = item;

newNode->next = top;

top = newNode;

printf("%d pushed to the stack\n", item);

int pop() {
if (top == NULL) {

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

return -1;

struct Node *temp = top;

int item = temp->data;

top = top->next;

free(temp);

printf("%d popped from the stack\n", item);

return item;

int main() {

push(10);

push(20);

push(30);

pop();

pop();

pop();

return 0;

```

**Problem 3:**

Write a C program to evaluate a postfix expression using a stack. The postfix expression is provided as an array of strings, where each element is
either an operand or an operator.

```c

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#define MAX_SIZE 100


struct Stack {

int top;

int items[MAX_SIZE];

};

void push(struct Stack *stack, int item) {

if (stack->top == MAX_SIZE - 1) {

printf("Stack is full. Cannot push.\n");

return;

stack->items[++stack->top] = item;

int pop(struct Stack *stack) {

if (stack->top == -1) {

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

return -1;

return stack->items[stack->top--];

int evaluatePostfix(char *expression[], int size) {

struct Stack stack;

stack.top = -1;

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

if (isdigit(expression[i][0])) {

push(&stack, atoi(expression[i]));

} else {

int operand2 = pop(&stack);

int operand1 = pop(&stack);

switch (expression[i][0]) {

case '+':

push(&stack, operand1 + operand2);

break;
case '-':

push(&stack, operand1 - operand2);

break;

case '*':

push(&stack, operand1 * operand2);

break;

case '/':

push(&stack, operand1 / operand2);

break;

return pop(&stack);

int main() {

char *postfixExpression[] = {"3", "4", "2", "*", "+"};

int result = evaluatePostfix(postfixExpression, 5);

printf("Result of the postfix expression: %d\n", result);

return 0;

```
STRUCTURES
**Problem 1:**

Define a structure named "Person" with members name, age, and address. Write a C program to create an array of 3 Person structures and print
their details.

```c

#include <stdio.h>

struct Person {

char name[50];

int age;

char address[100];

};

int main() {

struct Person people[3];

// Input details for each person

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

printf("Enter details for Person %d:\n", i + 1);

printf("Name: ");

scanf("%s", people[i].name);

printf("Age: ");

scanf("%d", &people[i].age);

printf("Address: ");

scanf("%s", people[i].address);

// Display details of each person

printf("\nDetails of People:\n");

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

printf("Person %d:\n", i + 1);

printf("Name: %s\n", people[i].name);

printf("Age: %d\n", people[i].age);

printf("Address: %s\n", people[i].address);

printf("\n");

}
return 0;

```

**Problem 2**

Create a structure named "Employee" with members name, salary, and experience. Write a C program to read details of n employees and
display the names of employees with more than 5 years of experience and a salary greater than 50000

```c

#include <stdio.h>

struct Employee {

char name[50];

float salary;

int experience;

};

int main() {

int n;

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

scanf("%d", &n);

struct Employee employees[n];

// Input details for each employee

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

printf("Enter details for Employee %d:\n", i + 1);

printf("Name: ");

scanf("%s", employees[i].name);

printf("Salary: ");

scanf("%f", &employees[i].salary);

printf("Experience (in years): ");

scanf("%d", &employees[i].experience);

}
// Display names of employees with more than 5 years of experience and salary > 50000

printf("\nEmployees with more than 5 years of experience and salary > 50000:\n");

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

if (employees[i].experience > 5 && employees[i].salary > 50000) {

printf("%s\n", employees[i].name);

return 0;

**Problem 3**

Create a structure named "Book" with members title, author, and price. Write a C program to create an array of 5 Book structures, sort them
based on price in ascending order, and then print the details.

```c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct Book {

char title[100];

char author[100];

float price;

};

int comparePrices(const void *a, const void *b) {

return (*(struct Book *)a).price - (*(struct Book *)b).price;

int main() {
struct Book books[5];

// Input details for each book

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

printf("Enter details for Book %d:\n", i + 1);

printf("Title: ");

scanf("%s", books[i].title);

printf("Author: ");

scanf("%s", books[i].author);

printf("Price: ");

scanf("%f", &books[i].price);

// Sort books based on price

qsort(books, 5, sizeof(struct Book), comparePrices);

// Display details of each book after sorting

printf("\nDetails of Books (sorted by price):\n");

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

printf("Book %d:\n", i + 1);

printf("Title: %s\n", books[i].title);

printf("Author: %s\n", books[i].author);

printf("Price: %.2f\n", books[i].price);

printf("\n");

return 0;

```

**Problem 4**

Create a C program that defines a structure representing a book with attributes like title, author, and price. Allow the user to input information
for two books and display the details of the book with the higher price.

```c

#include <stdio.h>

struct Book {
char title[100];

char author[50];

float price;

};

int main() {

struct Book book1, book2;

printf("Enter details for Book 1:\n");

printf("Title: ");

scanf("%s", book1.title);

printf("Author: ");

scanf("%s", book1.author);

printf("Price: ");

scanf("%f", &book1.price);

printf("\nEnter details for Book 2:\n");

printf("Title: ");

scanf("%s", book2.title);

printf("Author: ");

scanf("%s", book2.author);

printf("Price: ");

scanf("%f", &book2.price);

if (book1.price > book2.price) {

printf("\nBook with higher price:\n");

printf("Title: %s\n", book1.title);

printf("Author: %s\n", book1.author);

printf("Price: %.2f\n", book1.price);

} else {

printf("\nBook with higher price:\n");

printf("Title: %s\n", book2.title);

printf("Author: %s\n", book2.author);

printf("Price: %.2f\n", book2.price);

return 0;

}
COMBINED

### Arrays and Pointers:

**Problem 1:**

Write a C program that takes an array of integers as input, and using pointers, find and print the sum of the even elements in the array.

```c

#include <stdio.h>

int main() {

int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int *ptr = arr;

int sum = 0;

while (*ptr != '\0') {

if (*ptr % 2 == 0) {

sum += *ptr;

ptr++;

printf("Sum of even elements: %d\n", sum);

return 0;

```

### Pointers and Structures:

**Problem 2:**

Create a structure named "Student" with members name and marks. Write a C program that uses pointers to an array of structures to find and
print the name of the student with the highest marks.

```c

#include <stdio.h>

struct Student {

char name[50];

float marks;
};

int main() {

struct Student students[] = {{"Alice", 85.5}, {"Bob", 92.0}, {"Charlie", 78.3}};

struct Student *ptr = students;

float maxMarks = 0;

char maxName[50];

for (int i = 0; i < sizeof(students) / sizeof(students[0]); i++) {

if (ptr->marks > maxMarks) {

maxMarks = ptr->marks;

strcpy(maxName, ptr->name);

ptr++;

printf("Student with the highest marks: %s\n", maxName);

return 0;

```

### Queues and Structures:

**Problem 3:**

Implement a queue in C using an array of structures. Each element in the queue represents a book with details such as title, author, and
publication year. Include functions for enqueue and dequeue operations.

```c

#include <stdio.h>

#include <string.h>

#define MAX_SIZE 5

struct Book {

char title[100];

char author[100];

int year;

};
struct Queue {

struct Book items[MAX_SIZE];

int front, rear;

};

void enqueue(struct Queue *q, struct Book *book) {

if ((q->rear + 1) % MAX_SIZE == q->front) {

printf("Queue is full. Cannot enqueue.\n");

return;

memcpy(&(q->items[q->rear]), book, sizeof(struct Book));

q->rear = (q->rear + 1) % MAX_SIZE;

printf("Book enqueued to the queue\n");

struct Book dequeue(struct Queue *q) {

struct Book emptyBook = {"", "", 0};

if (q->front == q->rear) {

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

return emptyBook;

struct Book book;

memcpy(&book, &(q->items[q->front]), sizeof(struct Book));

q->front = (q->front + 1) % MAX_SIZE;

printf("Book dequeued from the queue\n");

return book;

int main() {

struct Queue bookQueue = {{}, 0, 0};

struct Book book1 = {"The Catcher in the Rye", "J.D. Salinger", 1951};
struct Book book2 = {"To Kill a Mockingbird", "Harper Lee", 1960};

enqueue(&bookQueue, &book1);

enqueue(&bookQueue, &book2);

struct Book dequeuedBook = dequeue(&bookQueue);

printf("Dequeued Book Details:\n");

printf("Title: %s\n", dequeuedBook.title);

printf("Author: %s\n", dequeuedBook.author);

printf("Year: %d\n", dequeuedBook.year);

return 0;

```

### Stacks and Pointers:

**Problem 4:**

Write a C program to reverse a string using a stack implemented with pointers.

```c

#include <stdio.h>

#include <string.h>

#define MAX_SIZE 50

struct Stack {

char items[MAX_SIZE];

int top;

};

void push(struct Stack *stack, char item) {

if (stack->top == MAX_SIZE - 1) {

printf("Stack is full. Cannot push.\n");

return;

stack->items[++stack->top] = item;
}

char pop(struct Stack *stack) {

if (stack->top == -1) {

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

return '\0';

return stack->items[stack->top--];

void reverseString(char *str) {

struct Stack charStack = {'\0', -1};

for (int i = 0; i < strlen(str); i++) {

push(&charStack, str[i]);

for (int i = 0; i < strlen(str); i++) {

str[i] = pop(&charStack);

int main() {

char str[MAX_SIZE];

printf("Enter a string: ");

scanf("%s", str);

reverseString(str);

printf("Reversed string: %s\n", str);

return 0;

```

### Structures and Arrays:


**Problem 5:**

Create a structure named "Rectangle" with members length and width. Write a C program that uses an array of structures to find and print the
area of the rectangle with the maximum area.

```c

#include <stdio.h>

struct Rectangle {

float length;

float width;

};

int main() {

struct Rectangle rectangles[] = {{4.5, 6.0}, {3.0, 8.0}, {5.5, 4.0}};

int maxIndex = 0;

for (int i = 1; i < sizeof(rectangles) / sizeof(rectangles[0]); i++) {

if ((rectangles[i].length * rectangles[i].width) > (rectangles[maxIndex].length * rectangles[maxIndex].width)) {

maxIndex = i;

printf("Rectangle with the maximum area:\n");

printf("Length: %.2f\n", rectangles[maxIndex].length);

printf("Width: %.2f\n", rectangles[maxIndex].width);

printf("Area: %.2f\n", rectangles[maxIndex].length * rectangles[maxIndex].width);

return 0;

```

### Arrays and Structures:

**Problem 1:**

Create a structure named "Student" with members name, age, and marks. Write a C program that takes an array of students as input and finds
and prints the student with the highest marks.

```c

#include <stdio.h>
struct Student {

char name[50];

int age;

float marks;

};

int main() {

struct Student students[] = {{"Alice", 20, 85.5}, {"Bob", 22, 92.0}, {"Charlie", 21, 78.3}};

int n = sizeof(students) / sizeof(students[0]);

int maxIndex = 0;

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

if (students[i].marks > students[maxIndex].marks) {

maxIndex = i;

printf("Student with the highest marks:\n");

printf("Name: %s\n", students[maxIndex].name);

printf("Age: %d\n", students[maxIndex].age);

printf("Marks: %.2f\n", students[maxIndex].marks);

return 0;

```

### Pointers and Arrays:

**Problem 2:**

Write a C program to implement a function that takes an array of integers and a pointer to a function. The function pointer should determine
whether each element is even or odd, and the program should print the count of even and odd numbers in the array.

```c

#include <stdio.h>

void countEvenOdd(int arr[], int size, int (*isEven)(int)) {

int evenCount = 0, oddCount = 0;

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


if (isEven(arr[i])) {

evenCount++;

} else {

oddCount++;

printf("Count of even numbers: %d\n", evenCount);

printf("Count of odd numbers: %d\n", oddCount);

int isEven(int x) {

return x % 2 == 0;

int main() {

int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int size = sizeof(arr) / sizeof(arr[0]);

countEvenOdd(arr, size, isEven);

return 0;

```

### Queues and Pointers:

**Problem 3:**

Implement a priority queue in C using a linked list of structures. Each element in the queue represents a task with details such as priority and
description. Include functions for enqueue with priority and dequeue operations.

```c

#include <stdio.h>

#include <stdlib.h>

struct Task {

int priority;

char description[100];

struct Task *next;


};

struct Task *front = NULL;

void enqueueWithPriority(int priority, const char *description) {

struct Task *newTask = (struct Task *)malloc(sizeof(struct Task));

if (newTask == NULL) {

printf("Memory allocation failed. Cannot enqueue.\n");

return;

newTask->priority = priority;

strcpy(newTask->description, description);

newTask->next = NULL;

if (front == NULL || priority < front->priority) {

newTask->next = front;

front = newTask;

} else {

struct Task *temp = front;

while (temp->next != NULL && temp->next->priority <= priority) {

temp = temp->next;

newTask->next = temp->next;

temp->next = newTask;

printf("Task enqueued to the priority queue\n");

void dequeue() {

if (front == NULL) {

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

return;

struct Task *temp = front;

printf("Dequeued Task Details:\n");


printf("Priority: %d\n", temp->priority);

printf("Description: %s\n", temp->description);

front = front->next;

free(temp);

int main() {

enqueueWithPriority(2, "Complete report");

enqueueWithPriority(1, "Attend meeting");

enqueueWithPriority(3, "Submit proposal");

dequeue();

dequeue();

dequeue();

return 0;

```

### Stacks and Structures:

**Problem 4:**

Create a structure named "Person" with members name, age, and address. Write a C program that uses a stack implemented with an array of
structures to reverse the order of people in a list.

```c

#include <stdio.h>

#include <string.h>

#define MAX_SIZE 5

struct Person {

char name[50];

int age;

char address[100];

};
struct Stack {

struct Person items[MAX_SIZE];

int top;

};

void push(struct Stack *stack, const struct Person *person) {

if (stack->top == MAX_SIZE - 1) {

printf("Stack is full. Cannot push.\n");

return;

stack->items[++stack->top] = *person;

struct Person pop(struct Stack *stack) {

if (stack->top == -1) {

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

struct Person emptyPerson = {"", 0, ""};

return emptyPerson;

return stack->items[stack->top--];

int main() {

struct Stack personStack = {{}, -1};

struct Person person1 = {"Alice", 25, "123 Main St"};

struct Person person2 = {"Bob", 30, "456 Oak St"};

struct Person person3 = {"Charlie", 22, "789 Pine St"};

push(&personStack, &person1);

push(&personStack, &person2);

push(&personStack, &person3);

printf("Original Order:\n");

for (int i = 0; i <= personStack.top; i++) {

printf("%s\n", personStack.items[i].name);
}

printf("\nReversed Order:\n");

while (personStack.top != -1) {

printf("%s\n", pop(&personStack).name);

return 0;

```

### Structures and Pointers:

**Problem 5:**

Create a structure named "Rectangle" with members length and width. Write a C program that uses pointers to an array of structures to find
and print the area of the rectangle with the maximum area.

```c

#include <stdio.h>

struct Rectangle {

float length;

float width;

};

int main() {

struct Rectangle rectangles[] = {{4.5, 6.0}, {3.0, 8.0}, {5.5, 4.0}};

int n = sizeof(rectangles) / sizeof(rectangles[0]);

int maxIndex = 0;

struct Rectangle *ptr = rectangles;

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

if ((ptr + i)->length * (ptr + i)->width > (ptr + maxIndex)->length * (ptr +

maxIndex)->width) {

maxIndex = i;

}
printf("Rectangle with the maximum area:\n");

printf("Length: %.2f\n", (ptr + maxIndex)->length);

printf("Width: %.2f\n", (ptr + maxIndex)->width);

printf("Area: %.2f\n", (ptr + maxIndex)->length * (ptr + maxIndex)->width);

return 0;

```

### Problem 1: Arrays and Pointers

**Problem:**

Write a C program that takes two integer arrays of the same length as input and calculates the dot product of the two arrays using pointers.

**Solution:**

```c

#include <stdio.h>

int calculateDotProduct(int *arr1, int *arr2, int size) {

int dotProduct = 0;

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

dotProduct += *(arr1 + i) * *(arr2 + i);

return dotProduct;

int main() {

int size;

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

scanf("%d", &size);

int array1[size], array2[size];


printf("Enter elements for the first array:\n");

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

printf("Element %d: ", i + 1);

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

printf("Enter elements for the second array:\n");

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

printf("Element %d: ", i + 1);

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

int result = calculateDotProduct(array1, array2, size);

printf("Dot Product of the two arrays: %d\n", result);

return 0;

```

### Problem 2: Pointers and Structures

**Problem:**

Create a program that manages a student database using a structure. Allow the user to input information for multiple students, including name,
age, and grades. Then, calculate the average grade for each student and display the student with the highest average.

**Solution:**

```c

#include <stdio.h>

struct Student {

char name[50];

int age;

float grades[5];

float averageGrade;

};

void calculateAverage(struct Student *student) {


float sum = 0;

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

sum += student->grades[i];

student->averageGrade = sum / 5;

int main() {

int numStudents;

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

scanf("%d", &numStudents);

struct Student students[numStudents];

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

printf("Enter details for Student %d:\n", i + 1);

printf("Name: ");

scanf("%s", students[i].name);

printf("Age: ");

scanf("%d", &students[i].age);

printf("Enter grades for each subject:\n");

for (int j = 0; j < 5; j++) {

printf("Grade for Subject %d: ", j + 1);

scanf("%f", &students[i].grades[j]);

calculateAverage(&students[i]);

// Find and display the student with the highest average grade

int maxIndex = 0;

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

if (students[i].averageGrade > students[maxIndex].averageGrade) {

maxIndex = i;
}

printf("\nStudent with the highest average grade:\n");

printf("Name: %s\n", students[maxIndex].name);

printf("Age: %d\n", students[maxIndex].age);

printf("Average Grade: %.2f\n", students[maxIndex].averageGrade);

return 0;

```

### Problem 3: Queues and Arrays

**Problem:**

Implement a circular queue in C using an array to simulate a printer queue. Each element in the queue represents a print job with details such as
document name and the number of pages. Include functions for enqueue and dequeue operations.

**Solution:**

```c

#include <stdio.h>

#include <stdlib.h>

#define MAX_SIZE 5

struct PrintJob {

char documentName[50];

int numPages;

};

struct Queue {

struct PrintJob items[MAX_SIZE];

int front, rear;

};

void enqueue(struct Queue *q, const struct PrintJob *job) {

if ((q->rear + 1) % MAX_SIZE == q->front) {

printf("Queue is full. Cannot enqueue.\n");

return;
}

q->rear = (q->rear + 1) % MAX_SIZE;

q->items[q->rear] = *job;

printf("Print job enqueued: %s\n", job->documentName);

struct PrintJob dequeue(struct Queue *q) {

struct PrintJob emptyJob = {"", 0};

if (q->front == q->rear) {

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

return emptyJob;

q->front = (q->front + 1) % MAX_SIZE;

struct PrintJob job = q->items[q->front];

printf("Print job dequeued: %s\n", job.documentName);

return job;

int main() {

struct Queue printerQueue = {{}, 0, 0};

struct PrintJob job1 = {"Document1.pdf", 10};

struct PrintJob job2 = {"Document2.doc", 5};

struct PrintJob job3 = {"Document3.txt", 8};

enqueue(&printerQueue, &job1);

enqueue(&printerQueue, &job2);

enqueue(&printerQueue, &job3);

dequeue(&printerQueue);

dequeue(&printerQueue);

dequeue(&printerQueue);
return 0;

```

### Problem 4: Stacks and Structures

**Problem:**

Create a program to check whether a given expression containing parentheses is balanced using a stack. The program should handle multiple
types of parentheses, including (), [], and {}.

**Solution:**

```c

#include <stdio.h>

#include <string.h>

#define MAX_SIZE 50

struct Stack {

char items[MAX_SIZE];

int top;

};

void push(struct Stack *stack, char item) {

if (stack->top == MAX_SIZE - 1) {

printf("Stack is full. Cannot push.\n");

return;

stack->items[++stack->top] = item;

char pop(struct Stack *stack) {

if (stack->top == -1) {

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

return '\0';

return stack->items[stack->top--];

}
int isMatchingPair(char opening, char closing) {

return (opening == '(' && closing == ')') ||

(opening == '[' && closing == ']') ||

(opening == '{' && closing == '}');

int isBalanced(char expression[]) {

struct Stack charStack = {'\0', -1};

for (int i = 0; i < strlen(expression); i++) {

if (expression[i] == '(' || expression[i] == '[' || expression[i] == '{') {

push(&charStack, expression[i]);

} else if (expression[i] == ')' || expression[i] == ']' || expression[i] == '}') {

if (charStack.top == -1 || !isMatchingPair(pop(&charStack), expression[i])) {

return 0; // Unbalanced

return charStack.top == -1; // Balanced if stack is empty

int main() {

char expression[MAX_SIZE];

printf("Enter an expression with parentheses: ");

scanf("%s", expression);

if (isBalanced(expression)) {

printf("The expression is balanced.\n");

} else {

printf("The expression is not balanced.\n");

return 0;

```
### Problem 5: Structures and Arrays

**Problem:**

Create a program to manage a library catalog using structures. Each book in the catalog has details such as title, author, and ISBN. Allow the
user to input information for multiple books, then display the details of books with titles longer than 20 characters.

**Solution:**

```c

#include <stdio.h>

struct Book {

char title[100];

char author[50];

char isbn[13];

};

int main() {

int numBooks;

printf("Enter the number of books in the catalog: ");

scanf("%d", &numBooks);

struct Book libraryCatalog[numBooks];

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

printf("Enter details for Book %d:\n", i + 1);

printf("Title: ");

scanf("%s", libraryCatalog[i].title);

printf("Author: ");

scanf("%s", libraryCatalog[i].author);

printf("ISBN: ");

scanf("%s", libraryCatalog[i].isbn);

}
printf("\nBooks with titles longer than 20 characters:\n");

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

if (strlen(libraryCatalog[i].title) > 20) {

printf("Title: %s\n", libraryCatalog[i].title);

printf("Author: %s\n", libraryCatalog[i].author);

printf("ISBN: %s\n", libraryCatalog[i].isbn);

printf("-----------------------------\n");

return 0;

```

### Problem 8: Structures

**Problem:**

Create a C program that uses a structure to represent a point in 2D space (x, y). Allow the user to input the coordinates of two points, and
calculate and display the distance between them.

**Solution:**

```c

#include <stdio.h>

#include <math.h>

struct Point {

float x;

float y;

};

float calculateDistance(struct Point p1, struct Point p2) {

return sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));

int main() {

struct Point point1, point2;


printf("Enter coordinates for Point 1:\n");

printf("x: ");

scanf("%f", &point1.x);

printf("y: ");

scanf("%f", &point1.y);

printf("Enter coordinates for Point 2:\n");

printf("x: ");

scanf("%f", &point2.x);

printf("y: ");

scanf("%f", &point2.y);

float distance = calculateDistance(point1, point2);

printf("Distance between the points: %.2f\n", distance);

return 0;

```

### Problem 9: Functions

**Problem:**

Write a C program that calculates and prints the factorial of a given positive integer using a recursive function.

**Solution:**

```c

#include <stdio.h>

int factorial(int n) {

if (n == 0 || n == 1) {

return 1;

} else {

return n * factorial(n - 1);

int main() {

int num;
printf("Enter a positive integer: ");

scanf("%d", &num);

if (num < 0) {

printf("Factorial is undefined for negative numbers.\n");

} else {

int result = factorial(num);

printf("Factorial of %d: %d\n", num, result);

return 0;

```

### Problem 10: Arrays and Functions

**Problem:**

Write a C program that takes an array of integers as input, and a function to find and print the sum of the array elements.

**Solution:**

```c

#include <stdio.h>

int calculateSum(int arr[], int size) {

int sum = 0;

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

sum += arr[i];

return sum;

int main() {

int size;

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

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

printf("Enter elements for the array:\n");

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

printf("Element %d: ", i + 1);

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

int result = calculateSum(array, size);

printf("Sum of the array elements: %d\n", result);

return 0;

```

### Problem 11: Pointers and Structures

**Problem:**

Create a C program that uses a structure to represent a rectangle. Allow the user to input the length and width of multiple rectangles, and use
pointers to calculate and display the area of each rectangle.

**Solution:**

```c

#include <stdio.h>

struct Rectangle {

float length;

float width;

};

void calculateArea(struct Rectangle *rect) {

rect->length *= 2;

rect->width *= 2;

int main() {

int numRectangles;
printf("Enter the number of rectangles: ");

scanf("%d", &numRectangles);

struct Rectangle rectangles[numRectangles];

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

printf("Enter dimensions for Rectangle %d:\n", i + 1);

printf("Length: ");

scanf("%f", &rectangles[i].length);

printf("Width: ");

scanf("%f", &rectangles[i].width);

calculateArea(&rectangles[i]);

printf("\nRectangles with doubled dimensions:\n");

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

printf("Rectangle %d:\n", i + 1);

printf("Length: %.2f\n", rectangles[i].length);

printf("Width: %.2f\n", rectangles[i].width);

printf("Area: %.2f\n", rectangles[i].length * rectangles[i].width);

printf("-----------------------------\n");

return 0;

```

### Problem 12: Functions and Pointers

**Problem:**

Write a C program that uses a function to find and print the average of an array of floating-point numbers using pointers.

**Solution:**

```c

#include <stdio.h>

float calculateAverage(float *arr, int size) {


float sum = 0;

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

sum += *(arr + i);

return sum / size;

int main() {

int size;

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

scanf("%d", &size);

float array[size];

printf("Enter elements for the array:\n");

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

printf("Element %d: ", i + 1);

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

float result = calculateAverage(array, size);

printf("Average of the array elements: %.2f\n", result);

return 0;

```
### Problem 13: Arrays, Pointers, and Functions

**Problem:**

Write a C program that takes an array of integers as input and a function to check whether the array is sorted in ascending order. If not, the
program should sort the array using pointers.

**Solution:**

```c

#include <stdio.h>

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

printf("Array elements: ");

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

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

printf("\n");

int isSorted(int arr[], int size) {

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

if (arr[i - 1] > arr[i]) {

return 0; // Not sorted

return 1; // Sorted

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

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

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

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

// Swap using pointers

int temp = *(arr + j);

*(arr + j) = *(arr + j + 1);

*(arr + j + 1) = temp;

}
int main() {

int size;

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

scanf("%d", &size);

int array[size];

printf("Enter elements for the array:\n");

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

printf("Element %d: ", i + 1);

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

printArray(array, size);

if (!isSorted(array, size)) {

sortArray(array, size);

printf("Array is not sorted. Sorting...\n");

printArray(array, size);

} else {

printf("Array is already sorted.\n");

return 0;

```

### Problem 14: Structures, Pointers, and Functions

**Problem:**

Create a C program to manage a student database using structures. Allow the user to input information for multiple students, including name,
age, and grades. Use pointers to find and display the student with the highest average grade.

**Solution:**

```c

#include <stdio.h>
struct Student {

char name[50];

int age;

float grades[5];

float averageGrade;

};

void calculateAverage(struct Student *student) {

float sum = 0;

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

sum += student->grades[i];

student->averageGrade = sum / 5;

int main() {

int numStudents;

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

scanf("%d", &numStudents);

struct Student students[numStudents];

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

printf("Enter details for Student %d:\n", i + 1);

printf("Name: ");

scanf("%s", students[i].name);

printf("Age: ");

scanf("%d", &students[i].age);

printf("Enter grades for each subject:\n");

for (int j = 0; j < 5; j++) {

printf("Grade for Subject %d: ", j + 1);

scanf("%f", &students[i].grades[j]);

}
calculateAverage(&students[i]);

// Find and display the student with the highest average grade

int maxIndex = 0;

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

if (students[i].averageGrade > students[maxIndex].averageGrade) {

maxIndex = i;

printf("\nStudent with the highest average grade:\n");

printf("Name: %s\n", students[maxIndex].name);

printf("Age: %d\n", students[maxIndex].age);

printf("Average Grade: %.2f\n", students[maxIndex].averageGrade);

return 0;

```

### Problem 15: Stacks, Structures, and Functions

**Problem:**

Implement a program that uses a stack to check whether a given arithmetic expression is well-formed, considering parentheses, square brackets,
and curly braces.

**Solution:**

```c

#include <stdio.h>

#include <string.h>

#define MAX_SIZE 50

struct Stack {

char items[MAX_SIZE];

int top;

};

void push(struct Stack *stack, char item) {


if (stack->top == MAX_SIZE - 1) {

printf("Stack is full. Cannot push.\n");

return;

stack->items[++stack->top] = item;

char pop(struct Stack *stack) {

if (stack->top == -1) {

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

return '\0';

return stack->items[stack->top--];

int isMatchingPair(char opening, char closing) {

return (opening == '(' && closing == ')') ||

(opening == '[' && closing == ']') ||

(opening == '{' && closing == '}');

int isWellFormed(char expression[]) {

struct Stack charStack = {'\0', -1};

for (int i = 0; i < strlen(expression); i++) {

if (expression[i] == '(' || expression[i] == '[' || expression[i] == '{') {

push(&charStack, expression[i]);

} else if (expression[i] == ')' || expression[i] == ']' || expression[i] == '}') {

if (charStack.top == -1 || !isMatchingPair(pop(&charStack), expression[i])) {

return 0; // Not well-formed

return charStack.top == -1; // Well-formed if stack is empty

}
int main() {

char expression[MAX_SIZE];

printf("Enter an arithmetic expression: ");

scanf("%s", expression);

if (isWellFormed(expression)) {

printf("The expression is well-formed.\n");

} else {

printf("The expression is not well-formed.\n");

return 0;

```

You might also like