You are on page 1of 37

South Point Degree

College

Programming File
C programming

Submitted to: -
Submitted by:- Neha
Roll no.:- 9031999
Sr.no Name of practical Date of practical Signature

1. Write a program to find simple


interest and compound interest
2. Write a program to degree into
Fahrenheit and vice versa.
3. Writer a program to find largest
number between three numbers (if-
then else).

4. Write a program to concatenate two


strings
5. Write a program to check that the
input string is palindrome or not
6. Write a program for insertion and
deletion in an array
7. Write a program to implement linear
search
8. Write a program to implement binary
search
9. Write a program to short the given
no. in ascending order using bubble
short.

10. Write a program to add, subtract and


multiply two matrices.

11. Write a program to implement a


stack using array
12. Write a program to implement a
stack using linked list
13. Write a program implement a queue
using array
14. Write a program to implement a
circular queue using array
15. Write a program to implement a simple
linked list

16. Write a program to traverse a tree


1. Write a program to find simple interest and compound interest.
#include <stdio.h>

#include <math.h>

// Function to calculate simple interest

float calculateSimpleInterest(float principal, float rate, float time) {

return (principal * rate * time) / 100;

// Function to calculate compound interest

float calculateCompoundInterest(float principal, float rate, float time) {

return principal * (pow((1 + rate / 100), time)) - principal;

int main() {

float principal, rate, time;

// Input the principal amount, rate of interest, and time period

printf("Enter principal amount: ");

scanf("%f", &principal);

printf("Enter rate of interest: ");

scanf("%f", &rate);

printf("Enter time period (in years): ");

scanf("%f", &time);

// Calculate and display simple interest

float simpleInterest = calculateSimpleInterest(principal, rate, time);

printf("\nSimple Interest: %.2f\n", simpleInterest);

// Calculate and display compound interest


float compoundInterest = calculateCompoundInterest(principal, rate, time);

printf("Compound Interest: %.2f\n", compoundInterest);

return 0;

2.Write a program to degree into Fahrenheit and vice versa.

#include <stdio.h>

// Function to convert Celsius to Fahrenheit

float celsiusToFahrenheit(float celsius) {

return (celsius * 9 / 5) + 32;

// Function to convert Fahrenheit to Celsius

float fahrenheitToCelsius(float fahrenheit) {

return (fahrenheit - 32) * 5 / 9;

int main() {

float temperature;
int choice;

// Menu to choose the conversion

printf("Choose the conversion:\n");

printf("1. Celsius to Fahrenheit\n");

printf("2. Fahrenheit to Celsius\n");

printf("Enter your choice (1 or 2): ");

scanf("%d", &choice);

// Input temperature

printf("Enter the temperature: ");

scanf("%f", &temperature);

// Perform the conversion based on the user's choice

switch (choice) {

case 1:

printf("%.2f Celsius is %.2f Fahrenheit\n", temperature, celsiusToFahrenheit(temperature));

break;

case 2:

printf("%.2f Fahrenheit is %.2f Celsius\n", temperature, fahrenheitToCelsius(temperature));

break;

default:

printf("Invalid choice\n");

return 0;

}
3. Writer a program to find largest number between three numbers
(if-then else).
#include <stdio.h>

int main() {

int num1, num2, num3;

// Input three numbers

printf("Enter three numbers: ");

scanf("%d %d %d", &num1, &num2, &num3);

// Find the largest number using if-else statements

if (num1 >= num2 && num1 >= num3) {

printf("%d is the largest number\n", num1);

} else if (num2 >= num1 && num2 >= num3) {

printf("%d is the largest number\n", num2);

} else {

printf("%d is the largest number\n", num3);

return 0;

}
4. Write a program to concatenate two strings.
#include <stdio.h>

int main() {

char str1[100], str2[100];

// Input the first string

printf("Enter the first string: ");

scanf("%s", str1);

// Input the second string

printf("Enter the second string: ");

scanf("%s", str2);

// Concatenate the two strings

strcat(str1, str2);

// Display the concatenated string

printf("Concatenated string: %s\n", str1);

return 0;

}
5 .Write a program to check that the input string is palindrome or not.
#include <stdio.h>

#include <string.h>

// Function to check if a string is a palindrome

int isPalindrome(char str[]) {

int len = strlen(str);

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

if (str[i] != str[len - i - 1]) {

return 0; // Not a palindrome

return 1; // Palindrome

int main() {

char input[100];

// Input the string

printf("Enter a string: ");

fgets(input, sizeof(input), stdin);

// Remove newline character from the input

input[strcspn(input, "\n")] = '\0';

// Check if the string is a palindrome

if (isPalindrome(input)) {

printf("The string is a palindrome.\n");


} else {

printf("The string is not a palindrome.\n");

return 0;

6. Write a program for insertion and deletion in an array.


#include <stdio.h>

#include <stdlib.h>

int main() {

int a[100];

int element, i, loc, size, n = 0, j = 0;

// Input the size of an array

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

scanf("%d", &size);

// Input array elements

printf("Enter %d array elements:\n", size);

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


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

// Display the list before insertion

printf("\nList before Insertion: ");

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

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

// Input element and position for insertion

printf("\nEnter an element to insert: ");

scanf("%d", &element);

printf("Enter a position to insert an element %d: ", element);

scanf("%d", &loc);

loc--;

// Perform insertion

for (i = size - 1; i >= loc; i--) {

a[i + 1] = a[i];

a[loc] = element;

// Display the list after insertion

printf("\nList after Insertion: ");

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

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

}
// Input element for deletion

printf("\nEnter an element to delete: ");

scanf("%d", &n);

// Perform deletion

i = 0;

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

if (a[i] == n) {

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

a[j] = a[j + 1];

break;

// Display the list after deletion

printf("List after deletion: ");

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

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

return 0;

}
7. Write a program to implement linear search.
#include <stdio.h>

// Function to perform linear search

int linearSearch(int arr[], int size, int key) {

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

if (arr[i] == key) {

return i; // Return the index if the element is found

return -1; // Return -1 if the element is not found

int main() {

int arr[100];

int size, key;

// Input the size of the array

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

scanf("%d", &size);
// Input array elements

printf("Enter %d array elements:\n", size);

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

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

// Input the element to search

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

scanf("%d", &key);

// Perform linear search

int position = linearSearch(arr, size, key);

// Display the result

if (position != -1) {

printf("Element %d found at position %d.\n", key, position + 1); // Adding 1 to show position starting from 1

} else {

printf("Element %d not found in the array.\n", key);

return 0;

}
8. Write a program to implement binary search.
#include <stdio.h>

// Function to perform binary search

int binarySearch(int arr[], int left, int right, int key) {

while (left <= right) {

int mid = left + (right - left) / 2;

// Check if the key is present at the middle

if (arr[mid] == key) {

return mid; // Return the index if the element is found

// If the key is greater, ignore the left half

else if (arr[mid] < key) {

left = mid + 1;

// If the key is smaller, ignore the right half

else {

right = mid - 1;

return -1; // Return -1 if the element is not found

int main() {

int arr[100];

int size, key;

// Input the size of the array

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

scanf("%d", &size);

// Input sorted array elements


printf("Enter %d sorted array elements:\n", size);

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

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

// Input the element to search

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

scanf("%d", &key);

// Perform binary search

int position = binarySearch(arr, 0, size - 1, key);

// Display the result

if (position != -1) {

printf("Element %d found at position %d.\n", key, position + 1); // Adding 1 to show position starting from 1

} else {

printf("Element %d not found in the array.\n", key);

return 0;

}
9. Write a program to short the given no. in ascending order using bubble short.
#include <stdio.h>

// Function to perform Bubble Sort

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

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

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

// Swap if the element found is greater than the next element

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

// Swap arr[j] and arr[j + 1]

int temp = arr[j];

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

arr[j + 1] = temp;

int main() {

int arr[100];

int size;

// Input the size of the array

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

scanf("%d", &size);

// Input array elements

printf("Enter %d array elements:\n", size);

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

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

}
// Perform Bubble Sort

bubbleSort(arr, size);

// Display the sorted array

printf("Sorted Array in Ascending Order: ");

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

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

return 0;

10. Write a program to add, subtract and multiply two matrices.


#include <stdio.h>

void addMatrices(int mat1[10][10], int mat2[10][10], int result[10][10], int rows, int cols) { for (int i = 0; i < rows; i++) {

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

result[i][j] = mat1[i][j] + mat2[i][j];

void subtractMatrices(int mat1[10][10], int mat2[10][10], int result[10][10], int rows, int cols) { for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) { result[i][j] = mat1[i][j] - mat2[i][j];

}
void multiplyMatrices(int mat1[10][10], int mat2[10][10], int result[10][10], int rows1, int cols1, int rows2, int cols2) {

if (cols1 != rows2) {

printf("Multiplication not possible. Number of columns in the first matrix must be equal to the number of rows in the second
matrix.\n");

return;

for (int i = 0; i < rows1; i++) { for (int j = 0; j < cols2; j++) {

result[i][j] = 0;

for (int k = 0; k < cols1; k++) { result[i][j] += mat1[i][k] * mat2[k][j];

void displayMatrix(int mat[10][10], int rows, int cols) { for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) { printf("%d\t", mat[i][j]);

printf("\n");

int main() {

int mat1[10][10], mat2[10][10], result[10][10]; int rows1, cols1, rows2, cols2;

printf("Enter the number of rows and columns for the first matrix:\n"); scanf("%d%d", &rows1, &cols1);

printf("Enter elements for the first matrix:\n"); for (int i = 0; i < rows1; i++) {

for (int j = 0; j < cols1; j++) { scanf("%d", &mat1[i][j]);

printf("Enter the number of rows and columns for the second matrix:\n"); scanf("%d%d", &rows2, &cols2);

printf("Enter elements for the second matrix:\n"); for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) { scanf("%d", &mat2[i][j]);

printf("\nFirst Matrix:\n"); displayMatrix(mat1, rows1, cols1);

printf("\nSecond Matrix:\n"); displayMatrix(mat2, rows2, cols2);

addMatrices(mat1, mat2, result, rows1, cols1); printf("\nSum of Matrices:\n"); displayMatrix(result, rows1, cols1);

subtractMatrices(mat1, mat2, result, rows1, cols1); printf("\nDifference of Matrices:\n"); displayMatrix(result, rows1, cols1);

multiplyMatrices(mat1, mat2, result, rows1, cols1, rows2, cols2); printf("\nProduct of Matrices:\n");

displayMatrix(result, rows1, cols2); return 0;

}
11. Write a program to implement a stack using array.
#include<stdio.h>

int stack[100], choice, n, top, x, i

void push(void);

void pop(void);

void display(void);

int main() {

top = -1;

printf("\n Enter the size of STACK[MAX=100]: ");

scanf("%d", &n);

printf("\n\t STACK OPERATIONS USING ARRAY");

printf("\n\t ");

printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");

do {

printf("\n Enter the Choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

push();

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:
printf("\n\t EXIT POINT ");

break;

default:

printf("\n\t Please Enter a Valid Choice (1/2/3/4)");

} while (choice != 4);

return 0;

void push() {

if (top >= n - 1) {

printf("\n\tSTACK is overflow");

} else {

printf(" Enter a value to be pushed: ");

scanf("%d", &x);

top++;

stack[top] = x;

void pop() {

if (top <= -1) {

printf("\n\t Stack is underflow");

} else {

printf("\n\t The popped element is %d", stack[top]);

top--;

void display() {

if (top >= 0) {
printf("\n The elements in STACK \n");

for (i = top; i >= 0; i--)

printf("%d\n", stack[i]);

} else {

printf("\n The STACK is empty");

}
12. Write a program to implement a stack using linked list.
#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* head = NULL;

void push(int val) {

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

newNode->data = val;

newNode->next = head;

head = newNode;

void pop() {

struct Node* temp;

if (head == NULL)

printf("Stack is Empty\n");

else {

printf("Popped element = %d\n", head->data);

temp = head;

head = head->next;

free(temp);

void display() {

struct Node* temp = head;

while (temp != NULL) {


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

temp = temp->next;

printf("NULL\n");

int main() {

push(10);

push(20);

push(30);

printf("Linked List\n");

display();

pop();

printf("After the pop, the new linked list\n");

display();

pop();

printf("After the pop, the new linked list\n");

display();

return 0;

}
13.Write a program implement a queue using array.
#include <stdio.h>

#define MAX_SIZE 100

struct Queue {

int arr[MAX_SIZE];

int front, rear;

};

// Function to initialize a queue

void initializeQueue(struct Queue *queue) {

queue->front = -1;

queue->rear = -1;

// Function to check if the queue is empty

int isEmpty(struct Queue *queue) {

return queue->front == -1;

// Function to check if the queue is full

int isFull(struct Queue *queue) {

return (queue->rear + 1) % MAX_SIZE == queue->front;

// Function to enqueue an element

void enqueue(struct Queue *queue, int element) {

if (isFull(queue)) {

printf("Queue is full. Cannot enqueue element %d.\n", element);

} else {

if (isEmpty(queue)) {

queue->front = 0; // If the queue was empty, set the front to 0

}
queue->rear = (queue->rear + 1) % MAX_SIZE;

queue->arr[queue->rear] = element;

printf("Element %d enqueued into the queue.\n", element);

// Function to dequeue an element

void dequeue(struct Queue *queue) {

if (isEmpty(queue)) {

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

} else {

printf("Element %d dequeued from the queue.\n", queue->arr[queue->front]);

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

// If the queue had only one element, reset front and rear

queue->front = -1;

queue->rear = -1;

} else {

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

// Function to display the elements of the queue

void displayQueue(struct Queue *queue) {

if (isEmpty(queue)) {

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

} else {

printf("Queue elements: ");

int i = queue->front;

do {

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


i = (i + 1) % MAX_SIZE;

} while (i != (queue->rear + 1) % MAX_SIZE);

printf("\n");

int main() {

struct Queue queue;

initializeQueue(&queue);

enqueue(&queue, 10);

enqueue(&queue, 20);

enqueue(&queue, 30);

displayQueue(&queue);

dequeue(&queue);

displayQueue(&queue);

dequeue(&queue);

dequeue(&queue); // Trying to dequeue from an empty queue

displayQueue(&queue);

return 0;

}
14. Write a program to implement a circular queue using array.
#include <stdio.h>

#define MAX_SIZE 5

struct CircularQueue {

int arr[MAX_SIZE];

int front, rear;

};

// Function to initialize a circular queue

void initializeCircularQueue(struct CircularQueue *cq) {

cq->front = -1;

cq->rear = -1;

// Function to check if the circular queue is empty

int isEmpty(struct CircularQueue *cq) {

return cq->front == -1;

// Function to check if the circular queue is full

int isFull(struct CircularQueue *cq) {

return (cq->rear + 1) % MAX_SIZE == cq->front;

// Function to enqueue an element

void enqueue(struct CircularQueue *cq, int element) {

if (isFull(cq)) {

printf("Circular Queue is full. Cannot enqueue element %d.\n", element);

} else {

if (isEmpty(cq)) {

cq->front = 0; // If the queue was empty, set the front to 0

}
cq->rear = (cq->rear + 1) % MAX_SIZE;

cq->arr[cq->rear] = element;

printf("Element %d enqueued into the circular queue.\n", element);

// Function to dequeue an element

void dequeue(struct CircularQueue *cq) {

if (isEmpty(cq)) {

printf("Circular Queue is empty. Cannot dequeue from an empty circular queue.\n");

} else {

printf("Element %d dequeued from the circular queue.\n", cq->arr[cq->front]);

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

// If the queue had only one element, reset front and rear

cq->front = -1;

cq->rear = -1;

} else {

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

// Function to display the elements of the circular queue

void displayCircularQueue(struct CircularQueue *cq) {

if (isEmpty(cq)) {

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

} else {

printf("Circular Queue elements: ");

int i = cq->front;

do {

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


i = (i + 1) % MAX_SIZE;

} while (i != (cq->rear + 1) % MAX_SIZE);

printf("\n");

int main() {

struct CircularQueue cq;

initializeCircularQueue(&cq);

enqueue(&cq, 10);

enqueue(&cq, 20);

enqueue(&cq, 30);

displayCircularQueue(&cq);

dequeue(&cq);

displayCircularQueue(&cq);

enqueue(&cq, 40);

enqueue(&cq, 50);

enqueue(&cq, 60);

displayCircularQueue(&cq);

enqueue(&cq, 70); // Trying to enqueue into a full circular queue

dequeue(&cq);

return 0;

}
15. Write a program to implement a simple linked list
#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* createNode(int value) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); if (newNode == NULL) {

printf("Memory allocation failed!\n"); exit(1);

newNode->data = value; newNode->next = NULL; return newNode;

struct Node* insertAtBeginning(struct Node* head, int value) { struct Node* newNode = createNode(value);

newNode->next = head; return newNode;

void displayList(struct Node* head) { struct Node* current = head; printf("Linked List: ");
while (current != NULL) { printf("%d -> ", current->data); current = current->next;

printf("NULL\n");

void freeList(struct Node* head) { struct Node* current = head; while (current != NULL) {

struct Node* temp = current; current = current->next; free(temp);

int main() {

struct Node* head = NULL;

head = insertAtBeginning(head, 30); head = insertAtBeginning(head, 20); head = insertAtBeginning(head, 10);


displayList(head);

freeList(head); return 0;

16. Write a program to traverse a tree.


#include <stdio.h>

#include <stdlib.h>

// Structure for a binary tree node

struct TreeNode {

int data;

struct TreeNode* left;

struct TreeNode* right;

};

// Function to create a new node with the given data


struct TreeNode* createNode(int value) {

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

newNode->data = value;

newNode->left = NULL;

newNode->right = NULL;

return newNode;

// Function to perform in-order traversal of the binary tree

void inOrderTraversal(struct TreeNode* root) {

if (root != NULL) {

inOrderTraversal(root->left);

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

inOrderTraversal(root->right);

// Function to perform pre-order traversal of the binary tree

void preOrderTraversal(struct TreeNode* root) {

if (root != NULL) {

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

preOrderTraversal(root->left);

preOrderTraversal(root->right);

// Function to perform post-order traversal of the binary tree

void postOrderTraversal(struct TreeNode* root) {

if (root != NULL) {

postOrderTraversal(root->left);

postOrderTraversal(root->right);

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


}

int main() {

// Constructing a sample binary tree

struct TreeNode* root = createNode(1);

root->left = createNode(2);

root->right = createNode(3);

root->left->left = createNode(4);

root->left->right = createNode(5);

root->right->left = createNode(6);

root->right->right = createNode(7);

printf("In-Order Traversal: ");

inOrderTraversal(root);

printf("\n");

printf("Pre-Order Traversal: ");

preOrderTraversal(root);

printf("\n");

printf("Post-Order Traversal: ");

postOrderTraversal(root);

printf("\n");

return 0;

You might also like