0% found this document useful (0 votes)
30 views17 pages

Bavi

Uploaded by

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

Bavi

Uploaded by

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

Exp-4

#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
}
void inorderTraversal(struct Node* root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
void preorderTraversal(struct Node* root) {
if (root != NULL) {
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
}

void postorderTraversal(struct Node* root) {


if (root != NULL) {
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}
}

int main() {
struct Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5)
printf("Inorder Traversal: ");
inorderTraversal(root);
printf("\n")
printf("Preorder Traversal: ");
preorderTraversal(root);
printf("\n");
printf("Postorder Traversal: ");
postorderTraversal(root);
printf("\n")
return 0;
}
Exp-5
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
}
struct Node* insert(struct Node* node, int value) {
if (node == NULL) return createNode(value);
if (value < node->data)
node->left = insert(node->left, value);
else if (value > node->data)
node->right = insert(node->right, value;
return node;
}
struct Node* minValueNode(struct Node* node) {
struct Node* current = node;
while (current && current->left != NULL)
current = current->left;
return current;
}
struct Node* deleteNode(struct Node* 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 {
if (root->left == NULL) {
struct Node* temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL) {
struct Node* temp = root->left;
free(root);
return temp;
}
struct Node* temp = minValueNode(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
return root;
}
void inorderTraversal(struct Node* root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
int main() {
struct Node*
exp-3
#include <stdio.h>
#define MAX 5
int queue[MAX], front = -1, rear = -1;
void enqueue(int value) {
if (rear == MAX - 1)
printf("Queue Overflow\n");
else {
if (front == -1)
front = 0;
queue[++rear] = value;
printf("Inserted %d\n", value);
}
}
void dequeue() {
if (front == -1 || front > rear)
printf("Queue Underflow\n");
else {
printf("Removed %d\n", queue[front++]);
if (front > rear)
front = rear = -1;
}
}
void display() {
if (front == -1)
printf("Queue is empty\n");
else {
printf("Queue elements are:\n");
for (int i = front; i <= rear; i++)
printf("%d\n", queue[i]);
}
}
int main() {
enqueue(10);
enqueue(20);
display();
dequeue();
display();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* front = NULL;
struct Node* rear = NULL;
void enqueue(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
printf("Inserted %d\n", value);
}
void dequeue() {
if (front == NULL)
printf("Queue Underflow\n");
else {
struct Node* temp = front;
front = front->next;
if (front == NULL)
rear = NULL;
printf("Removed %d\n", temp->data);
free(temp);
}
}
void display() {
if (front == NULL)
printf("Queue is empty\n");
else {
struct Node* temp = front;
printf("Queue elements are:\n");
while (temp != NULL) {
printf("%d\n", temp->data);
temp = temp->next;
}
}
}
int main() {
enqueue(10);
enqueue(20);
display();
dequeue();
display();
return 0;
}
Exp-2
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};

struct Node* top = NULL;


void push(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = top;
top = newNode;
printf("Inserted %d\n", value);
}
void pop() {
if (top == NULL)
printf("Stack Underflow\n");
else {
struct Node* temp = top;
printf("Removed %d\n", top->data);
top = top->next;
free(temp);
}
}
void display() {
if (top == NULL)
printf("Stack is empty\n");
else {
struct Node* temp = top;
printf("Stack elements are:\n");
while (temp != NULL) {
printf("%d\n", temp->data);
temp = temp->next;
}
}
}
int main() {
push(10);
push(20);
display();
pop();
display();
return 0;
}
#include <stdio.h>
#define MAX 5
int stack[MAX], top = -1;
void push(int value) {
if (top == MAX - 1)
printf("Stack Overflow\n");
else {
stack[++top] = value;
printf("Inserted %d\n", value);
}
void pop() {
if (top == -1)
printf("Stack Underflow\n");
else
printf("Removed %d\n", stack[top--]);
}
void display() {
if (top == -1)
printf("Stack is empty\n");
else {
printf("Stack elements are:\n");
for (int i = top; i >= 0; i--)
printf("%d\n", stack[i]);
}
}
int main() {
push(10);
push(20);
display();
pop();
display();
return 0;
}
Exp-1
#include<stdio.h>
int main() {
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
// value of first is assigned to temp
temp = first;

// value of second is assigned to first


first = second;
// value of temp (initial value of first) is assigned to second
second = temp;
// %.2lf displays number up to 2 decimal points
printf("\nAfter swapping, first number = %.2lf\n", first);
printf("After swapping, second number = %.2lf", second);
return 0;
}
#include <stdio.h>
int main() {
int i, n;
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
// get no. of terms from user
printf("Enter the number of terms: ");
scanf("%d", &n);
// print the first two terms t1 and t2
printf("Fibonacci Series: %d, %d, ", t1, t2);
// print 3rd to nth terms
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
#include <stdio.h>
int main() {
float subject1, subject2, subject3, subject4, subject5;
float total, average;
// Input marks for 5 subjects
printf("Enter marks for subject 1: ");
scanf("%f", &subject1);
printf("Enter marks for subject 2: ");
scanf("%f", &subject2);
printf("Enter marks for subject 3: ");
scanf("%f", &subject3);
printf("Enter marks for subject 4: ");
scanf("%f", &subject4);
printf("Enter marks for subject 5: ");
scanf("%f", &subject5);
// Calculate total
total = subject1 + subject2 + subject3 + subject4 + subject5;
// Calculate average
average = total / 5;
// Print total and average
printf("Total marks: %.2f\n", total);
printf("Average marks: %.2f\n", average);
return 0;
}
#include <stdio.h>

int main() {
int num, i;
unsigned long long factorial = 1; // Factorial can grow large, so use 'unsigned
long long'

// Input a number
printf("Enter a number: ");
scanf("%d", &num);
// Ensure the input is non-negative
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
// Calculate factorial
for(i = 1; i <= num; i++) {
factorial *= i;
}

// Print the result


printf("Factorial of %d = %llu\n", num, factorial);
}

return 0;
}
#include <stdio.h>

int main() {
int i, num = 13;
// Print multiplication table for 13
printf("Multiplication Table for %d:\n", num);
for(i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}
#include <stdio.h>

int main() {
int num;

// Input a number
printf("Enter an integer: ");
scanf("%d", &num);

// Check if the number is even or odd


if (num % 2 == 0) {
printf("%d is an even number.\n", num);
} else {
printf("%d is an odd number.\n", num);
}

return 0;
}

You might also like