CDS LAB MANUAL
CDS LAB MANUAL
PREPARED BY
Mrs.SHEKINA.K, AP/CSE
INDEX
PAGE
S.NO DATE NAME OF THE EXPERIMENT SIGN
NO
EX:NO:1 A) ARMSTRONG NUMBER
AIM:
To write a C program to find whether a given number is Armstrong or not using while loop.
ALGORITHM:
❖ Start the program.
❖ Initialize sum to 0.
❖ Read a number from the user.
❖ Copy the given number into a variable.
❖ Repeat the steps 6 to 8 until the number is greater than 0.
❖ Extract the remainder of given number while dividing by 10.
❖ Calculate the cube value of remainder.
❖ Overwrite the sum by adding above result with available sum.
❖ Overwrite the number by divide with 10.
❖ Compare the sum and copy of the number.
❖ If they are equal print “Given Number is Armstrong” else print “Given Number is Not Armstrong”
❖ Stop the program.
PROGRAM:
#include <stdio.h>
#include<conio.h>
void main( )
{
int number, temp, remainder, sum=0;
clrscr();
printf("Enter a positive integer: ");
scanf("%d", &number);
temp=number;
while(temp!=0)
{
remainder=temp%10;
sum=sum+remainder*remainder*remainder;
temp=temp/10;
}
if(number==sum)
{
printf("%d is an Armstrong number",number);
}
else
{
printf("%d is not an Armstrong number",number);
}
getch( );
}
OUTPUT:
RESULT:
Thus the C program for finding whether a given number is Armstrong number or not using while loop was
coded and executed successfully.
EX.NO: FIBONACCI SERIES
AIM:
To write a C program to generate the first n terms of the Fibonacci series.
ALGORITHM:
❖ Read the number of terms n
❖ Initialize a 0, b 1
❖ print a and b values
❖ for i from 3 to n
• increment the i value
• c = a+b
• print c value
• a=b
• b=c
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,c,n,i;
clrscr();
printf(“Enter no. of terms:”);
scanf(“%d”, &n);
printf(“The Fibonacci sequence
is:”);
printf(“%d%d”, a,b);
for(i=3;i<=n;i++)
{
c=a+b;
printf(“%d”,c);
a=b;
b=c;
}
getch();
}
OUTPUT:
RESULT:
Thus the C program to generate the Fibonacci series is executed successfully.
EX.NO: SUM OF ‘N’ NUMBERS
AIM:
To write a C program to find the sum of „n‟ numbers using for loop.
ALGORITHM:
• Start the program.
• Declare the variables. Initialize sum=0.
• Read a number “n”.
• Calculate the sum using a for loop.
• Display the value of sum.
• Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main( )
{
int n,i,sum=0;
clrscr( );
printf("Enter a number");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
sum=sum+i;
}
printf("The sum is %d",sum);
getch( );
}
OUTPUT:
RESULT:
Thus the C program to find the sum of „n‟ numbers using for loop was coded and executed successfully
EX:NO LARGEST ELEMENT IN AN ARRAY
Aim:
To get the largest element of an array using function
Algorithm:
Step 1: Start the program
Step 2: Initialize the array elements
Step 3: Find the largest number of the array
Step 4: Display the largest number
Step 6: Stop the program
Program:
#include <stdio.h>
#include <math.h>
int largest(int arr[],int size)
{
int largest=arr[0];
for(int i=1;i<size;i++)
{
if(arr[i] > largest)
largest=arr[i];
}
return largest;
}
int main()
{
int size;
printf("Enter Size of Array: ");
scanf("%d",&size);
int arr[size];
printf("Enter Array Elements: ");
for(int i=0;i<size;i++)
scanf("%d",&arr[i]);
printf("Largest Element is: %d",largest(arr,size));
return 0;
}
Output:
Result:
Thus the C Program to display the largest number in an array using function has been executed and verified.
EX.NO: FACTORIAL OF A GIVEN NUMBER USING RECURSION
AIM:
To write a C program to find the factorial of a given integer by using recursive functions.
ALGORITHM:
❖ Define the recursive function
❖ Read the number n
❖ if n is equal to 0
❖ then print “factorial of 0 is 1”
❖ else call the recursive function
❖ print the factorial value.
PROGRAM:
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
RESULT:
Thus the factorial of a given number using recursive function is executed successfully.
EX.NO: MATRIX ADDITION
AIM:
To write a c program to perform matrix addition and matrix subtraction.
ALGORITHM:
• Start the program.
• Read the size of matrix.
• Read the elements of the two matrices using for loop.
• The two matrices are added using for loop and the result is stored in another matrix.
• The resultant matrix is displayed using for loop.
• Similarly the matrix subtraction is performed and the result is displayed.
• Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10], b[10][10], c[10][10], d[10][10],i, j, m, n;
clrscr();
printf("Enter the size of the matrix \n");
scanf("%d%d" , &m, &n);
printf("Enter matrix A : \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter matrix B : \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d", &b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j] = a[i][j] + b[i][j];
}}
printf("\n Addition of matrices is \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d \t", c[i][j]);
}
printf("\n");
}
getch();
}
OUTPUT:
RESULT:
Thus the C program to perform matrix addition was coded and executed successfully.
EX.NO: PRIME NUMBER OR NOT
AIM:
To write a C program to generate all the prime numbers between 1 and n is a value supplied by the user.
ALGORITHM:
❖ Read n value
❖ Initialize count =0
❖ for i in 2 to n
• for j in 1 to i
• if i mod j is equal to 0
• then increment count
• if count is equal to 2
• then print i value.
PROGRAM:
#incloude<stdio.h>
#Include<conio.h>
void main()
{
int i, j, n, count=0;
c lrscr();
printf(“Enter the limit:”);
scanf(“%d”, &n);
printf(“The prime numbers are:”);
for(i=2;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
printf(“%d\t”, i);
}
getch();
}
OUTPUT:
RESULT:
Thus a C program to generate prime number is executed successfully.
EXNO: MATRIX MULTIPLICATION
AIM:
To write a c program to perform 2 X 2 matrices multiplication.
ALGORITHM:
• Start the program
• Declarethevariables.
• Read the input for the matrices A and B.
• Print the matrices A and B.
• Multiplythe matrices A and B and print the result in a matrix C.
• Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
clrscr();
printf("\nEnter the row and column of first matrix:");
scanf("%d %d",&m,&n);
printf("\nEnter the row and column of second matrix:");
scanf("%d %d",&o,&p);
if(n!=o)
{
printf("Matrix mutiplication is not possible");
printf("\nColumn of first matrix must be same as row of second matrix");
}
else
{
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter the Second matrix->");
for(i=0;i<o;i++)
{
for(j=0;j<p;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\nThe First matrix is ");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
}
printf("\nThe Second matrix is ");
for(i=0;i<o;i++)
{
printf("\n");
for(j=0;j<p;j++)
{
printf("%d\t",b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
c[i][j]=0;
}
}
for(i=0;i<m;i++) //row of first matrix
{
for(j=0;j<p;j++) //column of second matrix
{
sum=0;
for(k=0;k<n;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
printf("\nThe multiplication of two matrix is ");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<p;j++)
{
printf("%d\t",c[i][j]);
}
}
getch();
}
OUTPUT:
RESULT:
Thus the c program to perform matrix multiplication was coded and executed successfully
EXNO: STUDENT’S DETAILS USING STRUCTURE
Aim:
To store student information in structure and display it
Algorithm:
Step 1: START
Step 2: Read student details like name, mark1,2,3
Step 3: Calculate total, and average
Step 4: Display the grade
Step 5: STOP
PROGRAM:
#include<stdio.h>
struct student
{
int roll_no, mark1, mark2, mark3, total;
float average;
char name[10],grade;
};
void struct_funct_student(struct student stu);
int main()
{
struct student stud;
printf("\nRoll No.=");
scanf("%d",&stud.roll_no);
printf("Name=");
scanf("%s",stud.name);
printf("Mark1=");
scanf("%d",&stud.mark1);
printf("Mark2=");
scanf("%d",&stud.mark2);
printf("Mark3=");
scanf("%d",&stud.mark3);
struct_funct_student(stud);
return 0;
}
void struct_funct_student( struct student stu)
{
stu.total = stu.mark1 + stu.mark2 + stu.mark3;
stu.average = stu.total / 3;
if(stu.average >= 90)
stu.grade='S';
else if(stu.average >= 80)
stu.grade='A';
else if(stu.average >= 70)
stu.grade='B';
else if(stu.average >= 60)
stu.grade='C';
else if(stu.average >= 50)
stu.grade='D';
else
stu.grade='F';
printf("\n ROLL NO. \t NAME \t TOTAL \t AVG \tGRADE \n");
printf("%d \t %s \t %d \t %f \t %c",
stu.roll_no,stu.name,stu.total,stu.average,stu.grade);
}
OUTPUT;
Result:
Thus the C Program to store and display student details using structures has been executed and the result was
verified.
EX.NO: C Program To Write All The Members Of An Array Of Structures To A File
UsingFwrite(). Read The Array From The File And Display On The
Screen
Aim:
To write C program to write all the members of an array of structures to a file using fwrite(). Read the array from the
file and display on the screen.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the variables.
STEP 3: Read the file name.
STEP 4: open the file to write the contents.
STEP 5: writing the file contents up to reach a particular condition.
STEP 6: Stop the program
PROGRAM:
#include <stdio.h>
struct student
{
char name[50];
int height;
};
int main(){
struct student stud1[5], stud2[5];
FILE *fptr;
int i;
fptr = fopen("file.txt","wb");
for(i = 0; i < 5; ++i)
{
fflush(stdin);
printf("Enter name: ");
gets(stud1[i].name);
printf("Enter height: ");
scanf("%d", &stud1[i].height);
}
fwrite(stud1, sizeof(stud1), 1, fptr);
fclose(fptr);
fptr = fopen("file.txt", "rb");
fread(stud2, sizeof(stud2), 1, fptr);
for(i = 0; i < 5; ++i)
{
printf("Name: %s\nHeight: %d", stud2[i].name, stud2[i].height);
}
fclose(fptr);
}
RESULT:
Thus a C program was coded and executed successfully.
lOMoAR cPSD| 35047263
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 10
void create();
void insert();
void deletion();
void search();
void display();
int a,b[20], n, p, e, f, i, pos;
void main()
{
int ch;
char g='y';
do
{
printf("\n main Menu");
printf("\n 1.Create \n 2.Delete \n 3.Search \n 4.Insert \n 5.Display\n
6.Exit \n"); printf("\n Enter your Choice");
scanf("%d", &ch);
switch(ch)
{
case 1:
create();
break;
case 2:
deletion();
break;
case 3:
search();
break;
case 4:
insert();
break;
lOMoAR cPSD| 35047263
case 5:
display();
break;
case 6:
exit();
break;
default:
printf("\n Enter the correct
choice:"); }
printf("\n Do u want to
continue:::"); scanf("\n%c",
&g);
}
while(g=='y'||g=='Y');
getch();
}
void create()
{
printf("\n Enter the number ofnodes");
scanf("%d", &n); for(i=0;i<n;i++)
{
printf("\n Enter the Element:",i+1);
scanf("%d", &b[i]);
}}
void deletion()
{
printf("\n Enter the position u want todelete::");
scanf("%d", &pos);
if(pos>=n)
{
printf("\n Invalid Location::");
}
else
{
for(i=pos+1;i<n;i++)
{
b[i-1]=b[i];
}
n--;
}
printf("\n The Elements after
deletion"); for(i=0;i<n;i++)
{
printf("\t%d", b[i]);
}}
void search()
{
printf("\n Enter the Element to besearched:");
lOMoAR cPSD| 35047263
scanf("%d", &e);
for(i=0;i<n;i++)
{
if(b[i]==e)
{
printf("Value is in the %d
Position", i); }
else
{
printf("Value %d is not in the
list::", e); continue;
}}}
void insert()
{
printf("\n Enter the position u need toinsert::");
scanf("%d", &pos);
if(pos>=n)
{
printf("\n invalid Location::");
}
else
{
for(i=MAX-1;i>=pos-1;i--)
{
b[i+1]=b[i];
}
printf("\n Enter the element to insert::\n");
scanf("%d",&p);
b[pos]=p;
n++;
}
printf("\n The list after insertion::\n");
display();
}
void display()
{
printf("\n The Elements of The list ADT are:");
for(i=0;i<n;i++)
{
printf("\n\n%d", b[i]);
}}
RESULT:
Thus the program to perform all the operations in List ADT using array is
successfully completed.
lOMoAR cPSD| 35047263
AIM:
To write a C program to implement stack ADT using array.
ALGORITHM:
1) Start the program.
2) Get the choice from the user.
3) If the choice is to push an element, get the element from the user and pushed it.
4) If the choice is to pop an element, get the element from the user and pop it.
5) If the choice is to display, then display the stack.
6) Stop the program.
PROGRAM:
#include<stdio.h>
int stack[100],
choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
//clrscr();
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:
{
lOMoAR cPSD| 35047263
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
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 under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
RESULT:
Thus the C program to implement stack ADT using array was coded and successfully.
lOMoAR cPSD| 35047263
AIM:
To write a C program to implement queue ADT using array.
ALGORITHM:
1) Start the program.
2) Get the choice from the user.
3) If the choice is to enqueue an element, get the element from the user and inserted it.
4) If the choice is to dequeue an element, get the element from the user and deleted it.
5) If the choice is to display, then display the queue.
6) Stop the program.
PROGRAM:
#include <stdio.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /* End of switch */
} /* End of while */
} /* End of main() */
void insert()
{
int add_item;
if (rear == MAX - 1)
lOMoAR cPSD| 35047263
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /* End of delete() */
void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}/
RESULT:
Thus the C program to implement queue ADT using array was coded and executed
successfully
lOMoAR cPSD| 35047263
AIM:
To write a C program to implement stack ADT using linked list.
ALGORITHM:
1) Start the program.
2) Get the choice from the user.
3) If the choice is to push an element, get the element from the user and pushed it.
4) If the choice is to pop an element, get the element from the user and pop it.
5) If the choice is to display, then display the stack.
6) Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include"stacklink.c"
void main()
{
int ch;
clrscr();
printf("\n1.Push\t");
printf("\n2.Pop\t");
printf("\n3.Display\t");
printf("\n4.Exit\t");
do
{
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
}
}while(ch<4);
getch();
}
//stacklink.c
void push( );
void pop( );
lOMoAR cPSD| 35047263
void display( );
struct node
{
int data;
struct node *next;
}*top=NULL;
void push()
{
int x;
struct node *newnode;
newnode=malloc(sizeof(struct node));
printf("\nEnter the number to be inserted:");
scanf("%d",&x);
newnode->data=x;
if(top==NULL)
{
newnode->next=NULL;
top=newnode;
}
else
{
newnode->next=top;
top=newnode;
}
printf("\nThe number inserted is %d",x);
return;
}
void pop( )
{
struct node *temp;
if(top==NULL)
{
printf("Stack underflow");
}
else
{
temp=top;
top=top->next;
printf("\nThe number deleted is %d",temp->data);
free(temp);
}
return;
}
void display()
{
struct node *i;
printf("\nThe elements of stack are:");
for(i=top;i!=NULL;i=i->next)
lOMoAR cPSD| 35047263
printf("%d\t",i->data);
if(top==NULL)
{
printf("\nStack empty");
}
return;
}
RESULT:
Thus the C program to implement stack ADT using linked list was coded and
executed successfully.
lOMoAR cPSD| 35047263
AIM:
To write a C program to implement queue ADT using linked list.
ALGORITHM:
1) Start the program.
2) Get the choice from the user.
3) If the choice is to enqueue an element, get the element from the user and insert it.
4) If the choice is to dequeuean element, get the element from the user and delete it.
5) If the choice is to display, then display the queue.
6) Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include"queuelink.c"
void main()
{
int ch;
clrscr();
printf("\n1.Enqueue\t");
printf("\n2.Dequeue\t");
printf("\n3.Display\t");
printf("\n4.Exit\t");
do
{
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
enqueue( );
break;
case 2:
dequeue( );
break;
case 3:
display( );
break;
case 4:
exit(0);
break;
}
}while(ch<4);
getch();
}
//queuelink.c
void enqueue( );
void dequeue( );
lOMoAR cPSD| 35047263
void display( );
struct node
{
int data;
struct node *next;
}*front=NULL,*rear=NULL;
void enqueue( )
{
int x;
struct node *newnode;
newnode=malloc(sizeof(struct node));
printf("\nEnter the number to insert");
scanf("%d",&x);
newnode->data=x;
if(rear==NULL)
{
front=newnode;
rear=newnode;
}
else
{
rear->next=newnode;
rear=newnode;
}
printf("\nThe element inserted is %d",newnode->data);
return;
}
void dequeue( )
{
struct node *temp;
if(front==NULL)
{
printf("queue underflow");
}
else
{
temp=front;
if(front==rear)
{
front=rear=NULL;
}
else
{
front=front->next;
}
printf("\nThe element deleted is %d",temp->data);
free(temp);
}
lOMoAR cPSD| 35047263
return;
}
void display( )
{
struct node *temp;
if(front==NULL)
{
printf("\nqueue empty");
}
else
{
temp=front;
printf("The elements of queue are:");
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->next;
}}
return;
}
RESULT:
Thus the C program to implement queue ADT using linked list was coded and
executed successfully.
lOMoAR cPSD| 35047263
AIM:
write a C program to convert the infix expression to postfix expression using Stack.
ALGORITHM:
1) Start the program by initializing an empty stack. This will be for holding our operators.
2) Read the string, getting the first operand and add it to the postfix string.
3) Read the next object in the string, normally an operator, and push the operator to the
stack.
a. If the operator is greater than the top most operator, push the current operator
and continue.
b. If the operator is lesser to the top most operator, pop the stack adding it to the
postfix string, and then push the current operator to the stack. Test the next
operator to see if it is also lesser and repeat step 2. If not continue.
c. If the operator is equal to the top most operator pop the top of the stack adding
it to the postfix string and then push the current operator to the stack.
4) Repeat these steps until all of the operands and operators are taken care of.
5) Afterwards look at the stack one last time to see if any operators remain, pop them all
and add them to the end of the postfix string.
6) Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define size 10
char stack[size];
int tos=0,ele;
void push();
char pop();
void show();
int isempty();
int isfull();
char infix[30],output[30];
int prec(char);
void main()
{
int i=0,j=0,length;
char temp;
clrscr();
printf("\nEnter an infix expression:");
scanf("%s",infix);
printf("\nThe infix expresson is %s",infix);
length=strlen(infix);
printf("\n\nInfix Length=%d",length);
for(i=0;i<length;i++)
lOMoAR cPSD| 35047263
{
if(infix[i]!='+' && infix[i]!='-' && infix[i]!='*' && infix[i]!='/' && infix[i]!='^' &&
infix[i]!=')' && infix[i]!='(' )
output[j++]=infix[i];
else
{
if(tos==0)
push(infix[i]);
else
{
if(infix[i]!=')' && infix[i]!='(')
{
if(prec(infix[i]) <= prec(stack[tos-1]))
{
temp=pop();
output[j++]=temp;
push(infix[i]);
}
else
push(infix[i]);
}
else
{
if(infix[i]=='(')
push(infix[i]);
if(infix[i]==')')
{
temp=pop();
while(temp!='(')
{
output[j++]=temp;
temp=pop();
}}}}}}
while(tos!=0)
output[j++]=pop();
printf("\n\nPostfix expression is: %s\n",output);
getch();
}
void push(int ele)
{
stack[tos]=ele;
tos++;
}
char pop()
{
tos--;
return(stack[tos]);
}
int prec(char symbol)
lOMoAR cPSD| 35047263
{
if(symbol== '(')
return 0;
if(symbol== ')')
return 0;
if(symbol=='+' || symbol=='-')
return 1;
if(symbol=='*' || symbol=='/')
return 2;
if(symbol=='^')
return 3;
return 0;
}
RESULT:
Thus the C program to convert the infix expression to postfix expression using Stack
was coded and executed successfully.
lOMoAR cPSD| 35047263
AIM:
To write a C program to perform FCFS scheduling.
ALGORITHM:
1) Start the program.
2) Using for loop get the process name and their burst time. Then store it in an array.
3) Depends upon the order of the arrival calculate the waiting time.
4) Print the waiting of each process.
5) Find the average waiting and turnaround time by using the formula.
1. Average time= Total waiting time / No. of process
2. Average Turnaround Time= Total Turnaround Time / No. of
Process
6) Print the average and turnaround time.
7) Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
printf("\nEnter Process Burst Time\n");
for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}
wt[0]=0; //waiting time for first process is 0
//calculating waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");
//calculating turnaround time
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
lOMoAR cPSD| 35047263
RESULT:
Thus the c program to perform FCFS Scheduling was coded and executed
successfully.
lOMoAR cPSD| 35047263
AIM:
To write a C program to implement binary search tree.
ALGORITHM:
1. Start the program.
2. Declare the structure for tree node.
3. Perform insert, delete, search and display operations using switch case and functions.
4. A newnode is created using malloc function.
5. Insert:
• if(parent==NULL)
▪ root=tmp;
• else if(item<parent->info)
▪ parent->lchild=tmp;
▪ else
6. Delate:
▪ parent->rchild=tmp;
▪ Deletion node with no children
• Deletion node with one child
• Deletion of node with two children
7. Search:
• if(item<ptr->info)
o ptr=ptr->lchild;
• else
o ptr=ptr->rchild;
8. Display:
• Inorder
▪ inorder(ptr->lchild);
▪ ptr->info;
▪ inorder(ptr->rchild);
• Preorder
▪ ptr->info;
▪ preorder(ptr->lchild);
▪ preorder(ptr->rchild);
• Postorder
▪ preorder(ptr->lchild);
▪ preorder(ptr->rchild);
▪ ptr->info;
9. Stop the program.
PROGRAM:
# include <stdio.h>
# include <malloc.h>
struct node
{
int info;
struct node *lchild;
struct node *rchild;
}*root;
void main()
{
lOMoAR cPSD| 35047263
int choice,num;
root=NULL;
clrscr();
while(1)
{
printf("\n\n");
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Search\n");
printf("4.Display\n");
printf("5.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
lOMoAR cPSD| 35047263
case 1:
printf("\nEnter the element to be inserted : ");
scanf("%d",&num);
insert(num);
break;
case 2:
printf("\nEnter the element to be deleted : ");
scanf("%d",&num);
del(num);
break;
case 3:
printf("\nEnter the element to be searchd : ");
scanf("%d",&num);
search(num);
break;
case 4:
printf("\nInorder traversal are:\n");
inorder(root);
printf("\npreorder traversal are:\n");
preorder(root);
printf("\nPostorder traversal are:\n");
postorder(root);
break;
case 5:
exit(0);
}/*End of switch */
}/*End of while */
}/*End of main()*/
find(int item,struct node **par,struct node **loc)
{
struct node *ptr,*ptrsave;
if(root==NULL) /*tree empty*/
{
*loc=NULL;
*par=NULL;
return;
}
if(item==root->info) /*item is at root*/
{
*loc=root;
*par=NULL;
return;
}
/*Initialize ptr and ptrsave*/
if(item<root->info)
ptr=root->lchild;
else
ptr=root->rchild;
lOMoAR cPSD| 35047263
ptrsave=root;
while(ptr!=NULL)
{
if(item==ptr->info)
{ *loc=ptr;
*par=ptrsave;
return;
}
ptrsave=ptr;
if(item<ptr->info)
ptr=ptr->lchild;
else
ptr=ptr->rchild;
}/*End of while */
*loc=NULL; /*item not found*/
*par=ptrsave;
}/*End of find()*/
insert(int item)
{ struct node *tmp,*parent,*location;
find(item,&parent,&location);
if(location!=NULL)
{
printf("Item already present");
return;
}
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=item;
tmp->lchild=NULL;
tmp->rchild=NULL;
if(parent==NULL)
root=tmp;
else if(item<parent->info)
parent->lchild=tmp;
else
parent->rchild=tmp;
}/*End of insert()*/
search(int item)
{
struct node *parent, *location;
find(item,&parent,&location);
if(location!=NULL)
printf("\nSuccessful search... %d is found\n",item);
else
printf("\nUnsuccessful search... %d is not found\n",item);
}
del(int item)
{
lOMoAR cPSD| 35047263
{
printf("Tree empty");
return;
}
find(item,&parent,&location);
if(location==NULL)
{
printf("Item not present in tree");
return;
}
if(location->lchild==NULL && location->rchild==NULL)
case_a(parent,location);
if(location->lchild!=NULL && location->rchild==NULL)
case_b(parent,location);
if(location->lchild==NULL && location->rchild!=NULL)
case_b(parent,location);
if(location->lchild!=NULL && location->rchild!=NULL)
case_c(parent,location);
free(location);
}/*End of del()*/
case_a(struct node *par,struct node *loc )
{
if(par==NULL) /*item to be deleted is root node*/
root=NULL;
else
if(loc==par->lchild)
par->lchild=NULL;
else
par->rchild=NULL;
}/*End of case_a()*/
case_b(struct node *par,struct node *loc)
{
struct node *child;
/*Initialize child*/
if(loc->lchild!=NULL) /*item to be deleted has lchild */
child=loc->lchild;
else /*item to be deleted has rchild */
child=loc->rchild;
if(par==NULL ) /*Item to be deleted is root node*/
root=child;
else
if( loc==par->lchild) /*item is lchild of its parent*/
par->lchild=child;
else /*item is rchild of its parent*/
par->rchild=child;
lOMoAR cPSD| 35047263
}/*End of case_b()*/
case_c(struct node *par,struct node *loc)
{
struct node *ptr,*ptrsave,*suc,*parsuc;
/*Find inorder successor and its parent*/
ptrsave=loc;
ptr=loc->rchild;
while(ptr->lchild!=NULL)
{
ptrsave=ptr;
ptr=ptr->lchild;
}
suc=ptr;
parsuc=ptrsave;
if(suc->lchild==NULL && suc->rchild==NULL)
case_a(parsuc,suc);
else
case_b(parsuc,suc);
if(par==NULL) /*if item to be deleted is root node */
root=suc;
else
if(loc==par->lchild)
par->lchild=suc;
else
par->rchild=suc;
suc->lchild=loc->lchild;
suc->rchild=loc->rchild;
}/*End of case_c()*/
preorder(struct node *ptr)
{
if(root==NULL)
{
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
printf("%d ",ptr->info);
preorder(ptr->lchild);
preorder(ptr->rchild);
}
}/*End of preorder()*/
inorder(struct node *ptr)
{
if(root==NULL)
{
printf("Tree is empty");
return;
}
lOMoAR cPSD| 35047263
if(ptr!=NULL)
{
inorder(ptr->lchild);
printf("%d ",ptr->info);
inorder(ptr->rchild);
}
}/*End of inorder()*/
postorder(struct node *ptr)
{
if(root==NULL)
{
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
postorder(ptr->lchild);
postorder(ptr->rchild);
printf("%d ",ptr->info);
}
}/*End of postorder()*/
RESULT:
Thus a C program to implement a binary search tree was coded and executed
successfully.
lOMoAR cPSD| 35047263
AIM:
To write a C program to perform linear search.
ALGORITHM:
1) Start the program.
2) Declare the variables.
3) Read the size of the array.
4) Read n numbers and the value to be searched.
5) If search value is equal to first element then print the number is available. Else search
with the second element and so on upto the end of the array.
6) Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[20],i,size,num;
clrscr();
printf("Enter the size of the array:");
scanf("%d",&size);
printf("Enter the elements\n");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number to be searched:");
scanf("%d",&num);
for(i=0;i<size;i++)
{
if(num==a[i])
{
printf("The number is available");
break;
}
}
if(num!=a[i])
{
printf("The number is not available");
}
getch();
}
RESULT:
Thus the C program to perform linear search was coded and executed successfully.
lOMoAR cPSD| 35047263
AIM:
To write a C program to perform binary search.
ALGORITHM:
1) Start the program.
2) Declare the variables.
3) Read the size of the array.
4) Read n numbers and the value to be searched.
5) If search value is equal to middle element then print value is found. If search value is
less than middle element then search left half of list with the same method. Else search
right half of list with the same method.
6) Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[20],i,size,num,first,middle,last;
clrscr( );
printf("Enter the size of the array:");
scanf("%d",&size);
printf("Enter the elements in asending order\n");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number to be searched:");
scanf("%d",&num);
first=0;
last=size-1;
while(first<=last)
{
middle=(first+last)/2;
if(num<a[middle])
{
last=middle-1;
}
else if(num>a[middle])
{
first=middle+1;
}
else
{
printf("The number is available at position %d",middle);
break;
}}
getch();
}
RESULT:
Thus the C program to perform binary search was coded and executed successfully.
lOMoAR cPSD| 35047263
AIM:
To write a C program to sort the elements using insertion sort.
ALGORITHM:
1) Start the program.
2) Declare the variables.
3) Read the size and the elements of an array to be sorted.
4) Implement the algorithm for insertion sort.
5) Display the sorted list of elements.
6) Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,j,k,n,temp,a[10];
clrscr();
printf("Enter the array size:");
scanf("%d",&n);
printf("Enter the elements to be sorted:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Steps of insertion sort\n");
for(i=0;i<n;i++)
{
temp=a[i];
for(j=i;j>0&&a[j-1]>temp;j--)
{
a[j]=a[j-1];
}
a[j]=temp;
printf("i=%d\t",i);
for(k=0;k<n;k++)
{
printf("%d\t",a[k]);
}
printf("\n");
}
printf("\nThe sorted elements are\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
getch();
}
RESULT:
Thus the C program to sort the elements using insertion sort was coded and
executed successfully.
lOMoAR cPSD| 35047263
AIM:
To write a C program to sort the elements using bubble sort.
ALGORITHM:
1) Start the program.
2) Declare the variables.
3) Enter the array size.
4) Enter the elements to be sorted.
5) Implement the algorithm for bubble sort.
6) Display the sorted list of elements.
7) Stop the program.
PROGRAM:
#include <stdio.h>
#include<conio.h>
void main()
{
int array[100], n, c, d, swap;
clrscr();
printf("Enter the number of elements to be sorted:");
scanf("%d",&n);
printf("Enter the elements:\n");
for(c=0;c<n;c++)
scanf("%d",&array[c]);
for(c=0;c<n-1;c++)
{
for(d=0;d<n-c-1;d++)
{
if(array[d]>array[d+1])
{
swap=array[d];
array[d]=array[d+1];
array[d+1] = swap;
}}}
printf("Sorted list in ascending order:\n");
for(c=0;c<n;c++)
printf("%d\t",array[c]);
getch();
}
RESULT:
Thus the C program to sort the elements using bubble sort was coded and executed
successfully.
lOMoAR cPSD| 35047263
AIM:
To write a C program to sort the elements using Quick Sort.
ALGORITHM:
1) Start the program.
2) Declare the variables.
3) Enter the maximum limit.
4) Enter the elements to be sorted.
5) Implement the randomized algorithm for quicksort.
6) Return the sorted list of elements.
7) Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
int a[20],i,j,pivot,n;
void quicksort(int a[ ],int left,int right);
void main( )
{
clrscr( );
printf("\n Enter the array size:");
scanf("%d",&n);
printf("\n Enter the elements to be sorted:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Steps of quick sort\n");
quicksort(a,0,n-1);
printf("The sorted elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
void quicksort(int a[],int first,int last)
{
int i,j,pivot,temp,k;
if(first<last)
{
pivot=first;
i=first+1;
j=last;
while(i<j)
{
while(a[pivot]>=a[i])
i++;
while(a[pivot]<a[j])
j--;
if(i<j)
{
temp=a[i];
lOMoAR cPSD| 35047263
a[i]=a[j];
a[j]=temp;
}
}
temp=a[pivot];
a[pivot]=a[j];
a[j]=temp;
for(k=0;k<n;k++)
{
printf("%d\t",a[k]);
}
printf("\n");
quicksort(a,first,j-1);
quicksort(a,j+1,last);
}}
RESULT:
Thus the C program to sort the elements using quick sort was coded and executed
successfully.
lOMoAR cPSD| 35047263
AIM:
To write a C program to sort the elements using merge sort.
ALGORITHM:
1) Start the program.
2) Declare the variables.
3) Enter the array size.
4) Enter the elements to be sorted.
5) Implement the algorithm for merge sort.
6) Display the sorted list of elements.
7) Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
void main()
{
int a[30],n,i;
clrscr();
printf("Enter the no. of elements:");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
mergesort(a,0,n-1);
printf("Sorted array is :\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
getch();
}
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,mid+1,j);
}
}
void merge(int a[],int i1,int j1,int i2,int j2)
{
int temp[50];
lOMoAR cPSD| 35047263
int i,j,k;
i=i1;
j=i2;
k=0;
while(i<=j1 && j<=j2)
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=j1)
temp[k++]=a[i++];
while(j<=j2)
temp[k++]=a[j++];
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}
RESULT:
Thus the C program to sort the elements using merge sort was coded and executed
successfully.