[go: up one dir, main page]

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

C Prom 2

The document contains code snippets for various C programs. The first program defines a structure to store student details like name, registration number, age and marks. It takes student details as input and prints the details. The second program implements binary search on an integer array. The third program reads contents of a file and prints it. The remaining programs implement binary tree, file handling, queue, binary search tree and other data structures concepts in C.

Uploaded by

joshua18012005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views23 pages

C Prom 2

The document contains code snippets for various C programs. The first program defines a structure to store student details like name, registration number, age and marks. It takes student details as input and prints the details. The second program implements binary search on an integer array. The third program reads contents of a file and prints it. The remaining programs implement binary tree, file handling, queue, binary search tree and other data structures concepts in C.

Uploaded by

joshua18012005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

11.

a) program:
#include <stdio.h>

// Define a structure for student details


struct Student {
char name[50];
int regNumber;
int age;
float mark1, mark2, mark3;
};

int main() {
// Declare a variable of type struct Student
struct Student student;

// Input student details


printf("Enter student name: ");
scanf("%s", student.name);

printf("Enter register number: ");


scanf("%d", &student.regNumber);

printf("Enter age: ");


scanf("%d", &student.age);

printf("Enter marks for subject 1: ");


scanf("%f", &student.mark1);

printf("Enter marks for subject 2: ");


scanf("%f", &student.mark2);

printf("Enter marks for subject 3: ");


scanf("%f", &student.mark3);

// Display student details


printf("\nStudent Details:\n");
printf("Name: %s\n", student.name);
printf("Register Number: %d\n", student.regNumber);
printf("Age: %d\n", student.age);
printf("Mark 1: %.2f\n", student.mark1);
printf("Mark 2: %.2f\n", student.mark2);
printf("Mark 3: %.2f\n", student.mark3);

return 0;
}
o/p:
Enter student name: John Doe
Enter register number: 12345
Enter age: 20
Enter marks for subject 1: 78.5
Enter marks for subject 2: 92.0
Enter marks for subject 3: 85.5

Student Details:
Name: John
Register Number: 12345
Age: 20
Mark 1: 78.50
Mark 2: 92.00
Mark 3: 85.50

11.b) program
#include <stdio.h>

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


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

// Check if x is present at mid


if (arr[mid] == x)
return mid;

// If x greater, ignore left half


if (arr[mid] < x)
left = mid + 1;

// If x is smaller, ignore right half


else
right = mid - 1;
}

// If we reach here, then the element was not present


return -1;
}

int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;

int result = binarySearch(arr, 0, n - 1, x);

if (result == -1)
printf("Element is not present in array\n");
else
printf("Element is present at index %d\n", result);

return 0;
}
o/p:
Element is present at index 3
12.a) program:
#include <stdio.h>

int main() {
FILE *file;
char filename[100];
char ch;

// Get the filename from the user


printf("Enter the filename: ");
scanf("%s", filename);

// Open the file for reading


file = fopen(filename, "r");
// Check if the file exists
if (file == NULL) {
printf("Error opening the file %s\n", filename);
return 1; // Exit the program with an error code
}

// Read and display the contents of the file


printf("Contents of %s:\n", filename);
while ((ch = fgetc(file)) != EOF) {
putchar(ch); // Print each character to the console
}

// Close the file


fclose(file);

return 0; // Exit the program successfully


}
o/p:
Enter the filename: example.txt
Contents of example.txt:
This is an example file.
It contains some text.
12.b) program:
#include <stdio.h>
#include <stdlib.h>

// Define a structure for a binary tree node


struct TreeNode {
int data;
struct TreeNode* left;
struct TreeNode* right;
};

// Function to create a new node with given data


struct TreeNode* createNode(int data) {
struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}

// Function to insert left subtree for a given node


void insertLeft(struct TreeNode* parent, int data) {
if (parent == NULL) {
printf("Cannot insert left child. Parent node is NULL.\n");
return;
}

if (parent->left != NULL) {
printf("Left child already exists. Cannot insert.\n");
return;
}

parent->left = createNode(data);
}

// Function to insert right subtree for a given node


void insertRight(struct TreeNode* parent, int data) {
if (parent == NULL) {
printf("Cannot insert right child. Parent node is NULL.\n");
return;
}

if (parent->right != NULL) {
printf("Right child already exists. Cannot insert.\n");
return;
}

parent->right = createNode(data);
}

// Function to print the binary tree (in-order traversal)


void inOrderTraversal(struct TreeNode* root) {
if (root != NULL) {
inOrderTraversal(root->left);
printf("%d ", root->data);
inOrderTraversal(root->right);
}
}

int main() {
// Create the root node
struct TreeNode* root = createNode(1);

// Insert left and right subtrees


insertLeft(root, 2);
insertRight(root, 3);
// Insert left and right subtrees for the left child
insertLeft(root->left, 4);
insertRight(root->left, 5);

// Print the binary tree using in-order traversal


printf("In-order traversal of the binary tree: ");
inOrderTraversal(root);
printf("\n");

return 0;
}
o/p:
In-order traversal of the binary tree: 4 2 5 1 3
13.a) program:
#include <stdio.h>

// Function to calculate the area of a rectangle


float calculateArea(float length, float width) {
return length * width;
}

// Function to calculate the perimeter of a rectangle


float calculatePerimeter(float length, float width) {
return 2 * (length + width);
}

int main() {
float length, width;
// Get user input for length and width
printf("Enter the length of the rectangle: ");
scanf("%f", &length);

printf("Enter the width of the rectangle: ");


scanf("%f", &width);

// Calculate area and perimeter


float area = calculateArea(length, width);
float perimeter = calculatePerimeter(length, width);

// Display the results


printf("Area of the rectangle: %.2f\n", area);
printf("Perimeter of the rectangle: %.2f\n", perimeter);

return 0;
}
o/p:
Enter the length of the rectangle: 5.5
Enter the width of the rectangle: 3.2
Area of the rectangle: 17.60
Perimeter of the rectangle: 17.40
13.b)program:
#include <stdio.h>
#include <stdlib.h>

// Define a structure for a match


struct Match {
int team1;
int team2;
};

// Define a structure for a queue node


struct Node {
struct Match data;
struct Node* next;
};

// Define a structure for a queue


struct Queue {
struct Node *front, *rear;
};

// Function to create a new queue node


struct Node* createNode(struct Match match) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = match;
newNode->next = NULL;
return newNode;
}

// Function to create an empty queue


struct Queue* createQueue() {
struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));
queue->front = queue->rear = NULL;
return queue;
}
// Function to enqueue a match into the queue
void enqueue(struct Queue* queue, struct Match match) {
struct Node* newNode = createNode(match);
if (queue->rear == NULL) {
queue->front = queue->rear = newNode;
return;
}
queue->rear->next = newNode;
queue->rear = newNode;
}

// Function to dequeue a match from the queue


struct Match dequeue(struct Queue* queue) {
if (queue->front == NULL) {
struct Match emptyMatch = {-1, -1}; // Return an empty match if the queue is empty
return emptyMatch;
}

struct Node* temp = queue->front;


struct Match match = temp->data;
queue->front = temp->next;

if (queue->front == NULL)
queue->rear = NULL;

free(temp);
return match;
}
// Function to display the FIFA schedule
void displaySchedule(struct Queue* scheduleQueue, int numTeams) {
while (scheduleQueue->front != NULL) {
struct Match match = dequeue(scheduleQueue);
printf("Match: Team %d vs Team %d\n", match.team1, match.team2);
}
}

int main() {
int numTeams;

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


scanf("%d", &numTeams);

// Create a queue for the FIFA schedule


struct Queue* scheduleQueue = createQueue();

// Generate the schedule using a round-robin approach


for (int i = 1; i <= numTeams; i++) {
for (int j = i + 1; j <= numTeams; j++) {
struct Match match = {i, j};
enqueue(scheduleQueue, match);
}
}

// Display the FIFA schedule


printf("FIFA Schedule:\n");
displaySchedule(scheduleQueue, numTeams);
return 0;
}
o/p:
Enter the number of teams: 4
FIFA Schedule:
Match: Team 1 vs Team 2
Match: Team 1 vs Team 3
Match: Team 1 vs Team 4
Match: Team 2 vs Team 3
Match: Team 2 vs Team 4
Match: Team 3 vs Team 4

14.a)program:
#include <stdio.h>

// Function to calculate the sum of digits of a number


int calculateSumOfDigits(int number) {
int sum = 0;

while (number != 0) {
sum += number % 10; // Add the last digit to the sum
number /= 10; // Remove the last digit
}

return sum;
}

int main() {
int num;

// Get user input for the number


printf("Enter a number: ");
scanf("%d", &num);

// Calculate the sum of digits


int sum = calculateSumOfDigits(num);

// Display the result


printf("Sum of digits of %d is: %d\n", num, sum);

return 0;
}
o/p:
Enter a number: 12345
Sum of digits of 12345 is: 15

14.b) program:
#include <stdio.h>
#include <stdlib.h>

// Define a structure for a binary search tree node


struct TreeNode {
int data;
struct TreeNode* left;
struct TreeNode* right;
};
// Function to create a new BST node
struct TreeNode* createNode(int key) {
struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));
newNode->data = key;
newNode->left = newNode->right = NULL;
return newNode;
}

// Function to insert a key into the BST


struct TreeNode* insert(struct TreeNode* root, int key) {
if (root == NULL)
return createNode(key);

if (key < root->data)


root->left = insert(root->left, key);
else if (key > root->data)
root->right = insert(root->right, key);

return root;
}

// Function to find the minimum value in a BST


struct TreeNode* findMin(struct TreeNode* root) {
while (root->left != NULL)
root = root->left;
return root;
}

// Function to find the maximum value in a BST


struct TreeNode* findMax(struct TreeNode* root) {
while (root->right != NULL)
root = root->right;
return root;
}

// Function to perform in-order traversal of the BST


void inOrderTraversal(struct TreeNode* root) {
if (root != NULL) {
inOrderTraversal(root->left);
printf("%d ", root->data);
inOrderTraversal(root->right);
}
}

int main() {
struct TreeNode* root = NULL;

// Insert keys into the BST


root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);

// Display the in-order traversal of the BST


printf("In-order traversal of the BST: ");
inOrderTraversal(root);
printf("\n");

// Find the minimum and maximum values in the BST


struct TreeNode* minNode = findMin(root);
struct TreeNode* maxNode = findMax(root);

printf("Minimum value in the BST: %d\n", minNode->data);


printf("Maximum value in the BST: %d\n", maxNode->data);

return 0;
}
o/p:
In-order traversal of the BST: 20 30 40 50 60 70 80
Minimum value in the BST: 20
Maximum value in the BST: 80
15.a)program:
#include <stdio.h>

int main() {
FILE *file;
char data[100];

// Get user input


printf("Enter data to write into file: ");
fgets(data, sizeof(data), stdin);

// Open the file for writing


file = fopen("file1.txt", "w");
// Check if the file is opened successfully
if (file == NULL) {
printf("Error opening the file.\n");
return 1; // Exit the program with an error code
}

// Write data to the file


fprintf(file, "%s", data);

// Close the file


fclose(file);

printf("Data written to file1.txt successfully.\n");

return 0;
}
o/p:
Enter data to write into file: This is a sample text.
Data written to file1.txt successfully.
15.b)program:
#include <stdio.h>
#include <stdlib.h>

// Define a structure for a binary search tree node


struct TreeNode {
int data;
struct TreeNode* left;
struct TreeNode* right;
};

// Function to create a new BST node


struct TreeNode* createNode(int key) {
struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));
newNode->data = key;
newNode->left = newNode->right = NULL;
return newNode;
}

// Function to insert a key into the BST


struct TreeNode* insert(struct TreeNode* root, int key) {
if (root == NULL)
return createNode(key);

if (key < root->data)


root->left = insert(root->left, key);
else if (key > root->data)
root->right = insert(root->right, key);

return root;
}

// Function to find the minimum value in a BST


struct TreeNode* findMin(struct TreeNode* root) {
while (root->left != NULL)
root = root->left;
return root;
}
// Function to delete a key from the BST
struct TreeNode* deleteNode(struct TreeNode* root, int key) {
if (root == NULL)
return root;

if (key < root->data)


root->left = deleteNode(root->left, key);
else if (key > root->data)
root->right = deleteNode(root->right, key);
else {
// Node with only one child or no child
if (root->left == NULL) {
struct TreeNode* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct TreeNode* temp = root->left;
free(root);
return temp;
}

// Node with two children: Get the inorder successor (smallest in the right subtree)
struct TreeNode* temp = findMin(root->right);

// Copy the inorder successor's content to this node


root->data = temp->data;

// Delete the inorder successor


root->right = deleteNode(root->right, temp->data);
}
return root;
}

// Function to make the BST empty


struct TreeNode* makeEmpty(struct TreeNode* root) {
if (root == NULL)
return NULL;

// Post-order traversal to delete nodes


root->left = makeEmpty(root->left);
root->right = makeEmpty(root->right);
free(root);

return NULL;
}

// Function to perform in-order traversal of the BST


void inOrderTraversal(struct TreeNode* root) {
if (root != NULL) {
inOrderTraversal(root->left);
printf("%d ", root->data);
inOrderTraversal(root->right);
}
}

int main() {
struct TreeNode* root = NULL;
// Insert keys into the BST
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);

// Display the in-order traversal of the original BST


printf("In-order traversal of the original BST: ");
inOrderTraversal(root);
printf("\n");

// Delete a node (e.g., delete 20)


root = deleteNode(root, 20);

// Display the in-order traversal after deletion


printf("In-order traversal after deleting node with key 20: ");
inOrderTraversal(root);
printf("\n");

// Make the BST empty


root = makeEmpty(root);

// Display the in-order traversal of the empty BST


printf("In-order traversal of the empty BST: ");
inOrderTraversal(root);
printf("\n");

return 0;
}
o/p:
In-order traversal of the original BST: 20 30 40 50 60 70 80
In-order traversal after deleting node with key 20: 30 40 50 60 70 80
In-order traversal of the empty BST:

You might also like