Manual Final
Manual Final
:
Write a C program to solve a quadric equation using the switch statement
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c;
float root1, root2, imaginary;
float d;
int x; // Variable to store the case number
d = (b * b) - (4 * a * c);
if (d > 0) {
x = 1;
} else if (d < 0) {
x = 2;
} else {
x = 3;
}
switch (x) {
case 1:
root1 = (-b + sqrt(d)) / (2 * a);
root2 = (-b - sqrt(d)) / (2 * a);
printf("Two distinct and real roots exist: %.2f and
%.2f", root1, root2);
break;
case 2:
printf("Roots are imaginary");
break;
case 3:
root1 = root2 = -b / (2 * a);
printf("Two equal and real roots exist: %.2f and %.2f",
root1, root2);
break;
}
return 0;
#include <stdio.h>
int main() {
int i, j;
return 0;
}
“Perform ADDITIONAL program during Lab”
Write a program to generate the pattern given below for required
number of lines
*
**
***
****
*****
2. Program 2a:
WAP to sort an array of numbers in ascending order
#include <stdio.h>
int main()
{
int a[10], n, temp, i, j, k;
if (n > 10) {
printf("Size should not exceed 10.\n");
return 1;
}
printf("Enter %d elements one by one:\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
// Bubble Sort
for (j = 0; j < n - 1; j++)
{
for (k = 0; k < n - 1 - j; k++)
{
if (a[k] > a[k + 1])
{
temp = a[k];
a[k] = a[k + 1];
a[k + 1] = temp;
}
}
}
return 0;
}
b)Sort the created database in increasing order of their salary using function
#include <stdio.h>
int main() {
int size = 3; // Number of employees
int i;
int emp_number_to_search = 1002; // Variable to search for an
employee
Employee employees[3]; // Declare an array of Employee structures
return 0;
}
}
void push()
{
if(top==SIZE-1)
{
printf("stack overflow \n");
}
else
{
printf("enter the element to be pushed:\n");
scanf("%d",&x);
top=top+1;
stack[top]=x;
}
}
void pop()
{
if(top==-1)
{
printf("stack underflow\n");
}
else
{
printf("the poped element is %d", stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("the element are:\n");
for(i=top; i>=0; i--)
{
printf("%d\n", stack[i]);
}
}
else{
printf("stack is empty");
}
}
Program 4
Given a valid parenthesized infix arithmetic expression convert it to postfix expression. The expression
consists of single character operands and the binary operators + (plus), -(minus), * (multiply) and /
(divide).
#include<stdio.h>
#include<ctype.h>
char stack[100];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}
int main()
{
char exp[100];
char *e, x;
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c ",pop());
}
return 0;
}
Program 5
Design, develop and execute a program in C to simulate the working
of a queue of integers using an array. Provide the following
operations.
a. Insert
b. Delete
c. Display
#include<stdio.h>
#define maxsize 3
int q[maxsize], front=0,rear=-1;
void insert()
{
int n;
if(rear==maxsize-1)
printf("\nQueue full\n");
else
{
printf("\nEnter the data to be added\n");
scanf("%d", &n);
q[++rear]=n;
}
}
void delete()
{
if(front>rear)
printf("\nQueue is empty\n");
else
{
printf("\n%d is deleted\n",q[front++]);
if(front>rear && rear==maxsize-1)
{
printf("\nReinit\n");
front=0; rear=-1;
}
}
}
void display()
{
int i;
if(front>rear)
printf("\nQueue is empty\n");
else
{
printf("\nQueue status is\n");
for(i=front;i<=rear;i++)
printf("%d\t",q[i]);
}
}
int main()
{
int ch;
while(1)
{
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
puts("\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insert(); break;
case 2:delete(); break;
case 3:display(); break;
case 4: return 0;
default :printf("\nInvalid choice\n");
}
}
}
“Perform ADDITIONAL program during Lab”
Design, develop and execute a program in C to simulate the working of a queue of charaters an array.
Provide the following operations.
Peek and search
Program 6
Simulate the working of Messaging system in which a message is placed in a
circular Queue by a message sender, a message is removed from the circular
queue by a message Receiver, which can also display the contents of the queue.
#include <stdio.h>
#define sz 5
int count =0;
int f=0,r=-1;
int q[sz];
void push(){
int item;
if(count == sz){
printf("Queue overflow");
return;
}
printf("Enter the element ");
scanf("%d",&item);
r = (r+1)%sz;
q[r]=item;
count++;
}
void pop(){
if(count == 0){
printf("queue underflow");
return;
}
printf("Item deleted is %d \n",q[f]);
f = (f+1)%sz;
count--;
}
void display(){
if(count == 0){
printf("Queue is empty \n");
return;
}
int j = f;
for(int i=0;i<count;i++){
printf("%d \t",q[j]);
j = (j+1)%sz;
}
printf("\n");
}
int main()
{
int option;
while(1){
printf("1.Push 2.Pop 3.Display 4. Exit \n");
printf("Enter the option ");
scanf("%d",&option);
switch(option){
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:return 0;
}
}
return 0;
}
“Perform ADDITIONAL program during Lab”
WAP for ticket booking system where booking requests from customers are placed in a circular
queue. When a ticket agent becomes available, the next request is processed. The operations
should include:
Program 7
Design, develop and execute a program in C to perform the following operation on Singly
linked list
a.Insert and Delete a node at the beginning of the list b. Display the list
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct link
{
int data;
struct link *next;
};
void main()
{
int ch;
printf("\nSINGLY LINKED LIST OPERATIONS\n");
printf("\n1.Addfirst\n2.DeleteFirst\n3.Display\n4.Exit\n");
do
{
printf("\nEnter your option:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
addfirst();
break;
case 2:
if(head==NULL)
printf("\nLIST IS EMPTY.....\n");
else
delfirst();
break;
case 3:
if(head==NULL)
printf("\nLIST IS EMPTY.....\n");
else
display();
break;
case 4:
exit(0);
break;
default:
printf("Invalid Choice\n");
}
}
while(ch<=4);
getch();
}
void addfirst()
{
node *temp;
temp=(node *)malloc(sizeof(node));
printf("\nEnter the data....\t");
scanf("%d",&temp->data);
temp->next=head;
head=temp;
}
void delfirst()
{
node *temp=head;
head=head->next;
printf("\nDeleted item is %d\n",temp->data);
free(temp);
}
void display()
{
node *cur=head;
printf("\nLINKED LIST : ");
while(cur!=NULL)
{
printf("\t%d",cur->data);
cur=cur->next;
}
}
Program 8:
Design, develop and execute a program in C to perform the following operation on Singly
linked list
a.Insert and Delete a node at the end of the list b. Display the list
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct link
{
int data;
struct link *next;
};
void main()
{
int ch;
printf("\nSINGLY LINKED LIST OPERATIONS\n");
printf("\n1.Addlast\n2.Deletelast\n3.Display\n4.Exit\n");
do
{
printf("\nEnter your option:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
addlast();
break;
case 2:
if(head==NULL)
printf("\nLIST IS EMPTY.....\n");
else
dellast();
break;
case 3:
if(head==NULL)
printf("\nLIST IS EMPTY.....\n");
else
display();
break;
case 4:
exit(0);
break;
default:
printf("Invalid Choice\n");
}
}
while(ch<=4);
getch();
}
void addlast()
{
node *temp,*cur;
temp=(node *)malloc(sizeof(node));
printf("\nEnter the data....");
scanf("%d",&temp->data);
temp->next=NULL;
if(head == NULL)
head =temp;
else
{
cur=head;
while(cur->next!=NULL)
cur=cur->next;
cur->next=temp;
}
}
void dellast()
{
node *temp,*cur=head;
if(cur->next==NULL)
{
printf("Deleted item is %d\n",cur->data);
free(cur);
head =NULL;
}
else
{
while(cur->next->next!=NULL)
cur=cur->next;
temp=cur->next;
cur->next=NULL;
printf("Deleted item is %d\n",temp->data);
free(temp);
}
}
void display()
{
node *cur=head;
printf("\nLINKED LIST : ");
while(cur!=NULL)
{
printf("\t%d",cur->data);
cur=cur->next;
}
Additional Program
WAP to insert and delete a node at the opposite end of a singly linked list and
display the same
Program 9:
Design, develop and execute a program in C to perform the following operation on doubly
linked list
a.Insert and Delete a node at the beginning of the list b. Display the list
#include <stdio.h>
#include <stdlib.h>
struct link {
struct link *prev;
int item;
struct link *next;
};
void main() {
int ch;
printf("\nDOUBLY LINKED LIST OPERATIONS\n");
printf("\n1.Addfirst\n2.DeleteFirst\n3.Display\n4.Exit\n");
do {
printf("\nEnter your option:\t");
scanf("%d", &ch);
switch (ch) {
case 1:
addfirst();
break;
case 2:
if (head == NULL)
printf("\nLIST IS EMPTY.....\n");
else
delfirst();
break;
case 3:
if (head == NULL)
printf("\nLIST IS EMPTY.....\n");
else
display();
break;
case 4:
exit(0);
break;
default:
printf("Invalid Choice\n");
}
} while (ch != 4);
}
void addfirst() {
node *temp;
temp = (node *)malloc(sizeof(node));
printf("\nEnter the data....\t");
scanf("%d", &temp->item);
temp->prev = NULL;
temp->next = head;
void delfirst() {
node *temp = head;
head = head->next;
if (head != NULL) // Check if the list is not becoming empty
after deletion
head->prev = NULL;
void display() {
node *cur = head;
printf("\nLINKED LIST :");
while (cur != NULL) {
printf("\t%d", cur->item);
cur = cur->next;
}
}
Additional WAP to insert and delete at the end
Program 10:
/*WAP to implement a circularly linked list by inserting elements at beginning of list
and removing from end of the list*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct num
{
int data;
struct num *link;
};
typedef struct num node;
node *head,*tail;// pointer to head node
Delete(int *x);
Insert(const int x) ;
void display();
}
//inserts elements at the beginning of the list
Insert(const int x)
{
node *q;
q= (node *)malloc(sizeof(node));
q->data = x;
if(head == NULL)
{
head=q;
tail = q;
}
else
{ q->link =head;
head = q;
}
tail->link = head;
}
void display()
{ node *p,*q;
p = head;
q=NULL;
while (p!=q)
{ printf("%d\t",p->data);
p = p->link;
q = head;
}
main()
{
int z;
Insert(2);
Insert(6);
Insert(5);
Insert(7);
printf("\nList is =" );
display();
Delete(&z);
printf("\nDeleted element is %d\n", z);
printf("\nList is =" );
display();
Delete(&z);
printf("\nDeleted element is %d\n", z);
printf("List is =" );
display();
}
Program 11:
/* WAP to create a binary tree and display the contents of the tree using
preorder, post order and inorder traversal techniques*/
#include <stdio.h>
#include <stdlib.h>
struct tree {
int info;
struct tree *left;
struct tree *right;
};
// Function declarations
struct tree* newNode(int data);
void inorder(struct tree *);
void postorder(struct tree *);
void preorder(struct tree *);
int main(void) {
struct tree *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
return 0;
}
Program 12
// WAP to create a Binary search tree and search a given data in the tree
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
/* A binary tree node has data, pointer to left child and a pointer to right child */
struct tree
{
int data;
struct tree* left;
struct tree* right;
};
struct tree *root = NULL;
main()
{
int num,info,i,s,found;
printf("\nenter the number of nodes required in the tree");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("\nenter the data");
scanf("%d",&info);
insert(info);
}
printf(" \ncontents of the tree using inorder traversal technique is");
inorder(root);
printf("\nenter the data to be searched");
scanf("%d",&s);
found = search(s);
if( found == 1)
printf( "\nelement being searched is found in the tree");
else
printf( "\nelement being searched is not found in the tree");
}