Cds Lab Manual
Cds Lab Manual
Algorithm:
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values for num1, num2.
Step 4: Add num1 and num2 and assign the result to a variable sum.
Step 5: Display sum
Step 6: Stop
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
int num1, num2, sum;
clrscr();
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
sum = num1 + num2;
printf("Sum of the entered numbers: %d", sum);
return 0;
}
Output:
Enter first number: 20
Enter second number: 19
Sum of the entered numbers: 39
Result:
Thus the given sum of two numbers is executed successfully.
ii) Write a program to find area and circumference of circle
Aim:-
To write a C program to find area and circumference of circle whose radius is entered
by the user
Flowchart:
Algorithm: -
Step 1: Start
Step 2: input R
Step 3: let pi = 3.14
Step 4: area = pi * R * R
Step 5: circum = 2 * pi * R
Step 6: print area, circum ,
Step 7: stop
Program:-
#include<stdio.h>
#include<conio.h>
#define PI 3.14
void main()
{
int rad;
float area, circum;
printf("\nEnter radius of circle: ");
scanf("%d", &rad);
area = PI * rad * rad;
printf("\nArea of circle : %f ", area);
circum= 2 * PI * rad;
printf("\nCircumference : %f ", circum);
getch();
}
Output:-
Enter radius of a circle : 10
Area of circle : 31.4
Circumference : 62.8
Result:-
Thus the given area and circumference of circle are executed successfully.
iii) Write a program to swap two numbers
Flowchart:-
Start
Read X and Y
Z=X
X=Y
Y=Z
Print X and Y
Stop
Algorithm:-
Step 1 : Start
Step 2 : READ X and Y
Step 3 :Z = X
Step 4 :X = Y
Step 5 :Y = Z
Step 6 : PRINT X , Y
Step 7 : Stop
Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
int x, y, z;
clrscr();
printf("\nEnter the values of x and y \n");
scanf("%d%d", &x,&y);
printf("Values before swapping - \n x = %d, y = %d \n\n", x, y);
z = x;
x = y;
y = z;
printf("Values after swapping - \n x = %d, y = %d \n", x, y);
getch();
}
Output:-
Enter the values of x and y
Values before swapping -
x = 11, y = 99
Result:-
Thus the given swapping of two numbers is executed successfully.
WEEK-1(B)
Flowchart:
Algorithm:
Step 1 : Start
Start 2 : Input a, b, c
Start 3 : if a > b goto step 4, otherwise goto step 5
Start 4 : if a > c goto step 6, otherwise goto step 8
Start 5 : if b > c goto step 7, otherwise goto step 8
Start 6 : Output "a is the largest", goto step 9
Start 7 : Output "b is the largest", goto step 9
Start 8 : Output " c is the largest", goto step 9
Start 9 : Stop
Program:
#include <stdio.h>
#include<conio.h>
void main()
{
int a, b, c;
clrscr();
printf("Enter three different numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c)
printf("%d is the largest number.", a);
else if (b >= a && b >= c)
printf("%d is the largest number.", b);
else
printf("%d is the largest number.", c);;
getch();
}
OUTPUT:
Enter three different numbers: 10 20 30
30 is the largest number.
RESULT:
Thus the largest among the three numbers program is executed successfully.
Aim:- To write a program in C to check whether the character entered by the user is
vowel or not.
Flowchart:
Algorithm:
Step 1: Start
Step 2: Declare character type variable ch
Step 3: Read ch from User
Step 4: // Checking both lower and upper case vowels.
IF (ch == 'a' || ch == 'A' ||
ch == 'e' || ch == 'E' ||
ch == 'i' || ch == 'I' ||
ch == 'o' || ch == 'O' ||
ch == 'u' || ch == 'U' )
Print "Vowel"
ELSE
Print "Consonant"
Step 5: Stop
Program :
#include <stdio.h>
#include<conio.h>
Void main()
{
charch;
clrscr();
printf("\n Enter any character : ");
scanf("%c", &ch);
switch(ch)
{
case ‘A’:
case ‘a’:
printf("\n %c is VOWEL", ch);
break;
case ‘E’:
case ‘e’:
printf("\n %c is VOWEL", ch);
break;
case ‘I’:
case ‘i’:
printf("\n %c is VOWEL", ch);
break;
case ‘O’:
case ‘o’:
printf("\n %c is VOWEL", ch);
break;
case ‘U’:
case ‘u’:
printf("\n %c is VOWEL", ch);
break;
default: printf("\n %c is not a vowel", ch);
}
getch();
}
Output:
Enter any character : j
j is not a vowel
WEEK - 2
Write C programs that use both recursive and non-recursive functions
i) To find the factorial of a given integer.
ii) To solve Towers of Hanoi problem.
Flowchart:
Algorithm:
Step 1: Start
Step 2: Read number n
Step 3: Call factorial(n)
Step 4: Print factorial f
Step 5: Stop
factorial(n)
Step 1: If n==1 then return 1
Step 2: Else
f=n*factorial(n-1)
Step 3: Return f
Program:
#include<stdio.h>
#include<conio.h>
int fact(int n)
{
if (n==0)
return 1;
return fact(n-1)*n;
}
void main()
{
int n;
clrscr();
printf("Enter a positive number : ");
scanf("%d",&n);
printf("\nThe factorial of a number is %d",fact(n));
getch();
}
Output:
Enter a positive number : 5
The factorial of a number is 120
Result:
Thus the Factorial of a given integer using recursion is executed successfully
Program:
#include <stdio.h>
#include<conio.h>
void main() {
int n, i;
unsigned long long fact = 1;
clrscr();
printf("Enter an integer: ");
scanf("%d", &n);
Output:
Enter an integer: 6
Factorial of 6 = 720
Result:
The Factorial of a given integer using non-recursion is executed successfully.
ii) To solve Towers of Hanoi problem.
Aim: To write a C program to solve the Tower of Hanoi problem
Flowchart:
Algorithm:
START
Procedure Hanoi(disk, source, dest, aux)
IF disk == 1, THEN
move disk from source to dest
ELSE
Hanoi(disk - 1, source, aux, dest) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, aux, dest, source) // Step 3
END IF
END Procedure
STOP
Program:
#include<stdio.h>
#include<conio.h>
void towers(int, char, char, char);
void main()
{
int n;
char source,dest, aux;
clrscr();
printf("Enter the number of disks : ");
scanf("%d", &n);
printf("\nSOURCE = %c DESTINATION = %c AUXILIARY = %c\n", 'A', 'C', 'B');
towers(n,'A','C','B');
getch();
}
if(n == 1)
printf("\nMove from %c to %c", source,dest);
else
{
towers(n-1,source, aux, dest);
printf("\nMove from %c to %c", source,dest);
towers(n-1,aux, dest, source);
}
printf("\n");
}
Output:
Enter the number of disks : 3
Move from A to C
Move from A to B
Move from C to B
Move from A to C
Move from B to A
Move from B to C
Move from A to C
Result:
The Tower of Hanoi problem is successfully executed.
WEEK-3
a) Write a C program to find both the largest and smallest number in a list of integers.
b) Write a C program that uses functions to perform the following:
i) Addition of Two Matrices ii) Multiplication of Two Matrices
a) Write a C program to find both the largest and smallest number in a list of integers.
Aim: To write a C program to find the largest and smallest number in a list of
integers.
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],i,n,large,small;
clrscr();
printf(“\nEnter the number of elements : “);
scanf(“%d”,&n);
printf(“\nInput the array elements : “);
for(i=0;i<n;++i)
scanf(“%d”,&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
getch();
}
Output:
Enter the number of elements : 5
Result:
Thus the largest and smallest integer from the list of integers in successfully executed.
C[i][j]=A[i][j] + B[i][j]
Set j=j+1
Step 7: Stop
Program:-
#include <stdio.h>
#include<conio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
clrscr();
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
}
}
getch();
}
Output:
Enter the number of rows (between 1 and 100): 2
Enter the number of columns (between 1 and 100): 2
6 8
10 12
Result:
The sum of two matrices is successfully executed.
ii) Multiplication of two matrices
Flowchart:
Algorithm:
Step 1: Start the Program.
Step 2: Enter the row and column of the first (a) matrix.
Step 3: Enter the row and column of the second (b) matrix.
Step 6: Print the elements of the first (a) matrix in matrix form.
Step 7: Print the elements of the second (b) matrix in matrix form.
Step 11: Multiply the first (a) and second (b) matrix and store the element in the third matrix
(c)
Program:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
clrscr();
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
Output:
enter the number of row=2
enter the number of column=2
enter the first matrix element=
1
2
3
4
enter the second matrix element=
1
2
3
4
multiply of the matrix=
7 10
15 22
void main()
{
char str[100], pat[100], insert_str[200];
inti,j,k,pos;
clrscr();
printf("\nEnter the main string: ");
gets(str);
for(i=0,k=0;i<pos;i++,k++)
insert_str[k]=str[i];
for(j=0;pat[j]!='\0';j++,k++)
insert_str[k]=pat[j];
for(;str[i]!='\0';i++,k++)
insert_str[k]=str[i];
insert_str[k]='\0';
getch();
}
Output:
Enter the main string: Hello Earth
Enter the string to be in inserted: Mars and
The main string after inserting string is: Hello Mars and Earth
Result:
The insertion of string in the main string is successfully executed.
ii) Given a string ―a$bcd./fg‖ find its reverse without changing the position of special
characters. (Example input:a@gh%;j and output:j@hg%;a)
Aim: To write a C program to find the reverse without changing the position of special
characters.
Program:
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
else
{
chartmp = str[l];
str[l] = str[r];
str[r] = tmp;
l++;
r--;
}
}
}
int main()
{
charstr[] = "a@gh%;j";
puts(str);
reverse(str);
puts(str);
return 0;
}
Output:
a@gh%;j
j@hg%;a
Result:
The reverse of the string without effecting the special character is successfully executed.
WEEK-5
From a given paragraph perform the following using built-in functions:
a. Find the total number of words.
b. Capitalize the first word of each sentence.
c. Replace a given word with another word.
Program:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
intwc,i=0;
charstr[100];
clrscr();
printf("Enter the string :");
gets(str);
while(str[i]!='\0')
{
if(str[i]==' ' &&str[i+1]!=' ')
wc++;
i++;
}
wc++;
printf("World count=%d",wc);
getch();
}
Output:
Enter the string :An Apple a day keeps the Doctor Away
World count=8
Result:
The word count in a string is successfully executed.
b. Capitalize the first word of each sentence.
Aim: To write a C Program to Capitalize the first word of each sentence
Program:
#include <stdio.h>
Int firstupper(char str[], int n) {
int i;
for(i = 0; i<n; i++) {
if (i == 0 &&str[i] != ' ' || str[i] != ' ' &&str[i-1] == ' ') {
if(str[i] >= 'a' &&str[i]<='z') {
str[i] = (char)(('A'-'a') + str[i] );
}
} else if (str[i] >= 'A' &&str[i] <= 'Z') {
str[i] = (char)(str[i] + ('a' - 'A'));
}
}
return 0;
}
void main() {
charstr[] = "my favourite fruit is apple";
int n = sizeof(str)/sizeof(str[0]);
firstupper(str, n);
printf("%s\n", str);
}
Output:
My Favourite Fruit Is Apple
#include<stdio.h>
#include<stdlib.h>
voidReadStrings();
voidPatternMatching(char[], char[], char[]);
char STR[100],PAT[100],REP[100];
int main()
{
ReadStrings(STR,PAT,REP);
PatternMatching(STR, PAT, REP);
}
voidReadStrings(char STR[100], char PAT[100], char REP[100])
{
printf("Enter the main string STR : ");
gets(STR);
printf("Enter the pattern string PAT : ");
gets(PAT);
printf("Enter the replace string REP : ");
gets(REP);
}
if(PAT[i] =='\0')
{
for(k=0;REP[k] !='\0';k++,j++)
RESULT[j]=REP[k];
i=0;
c=m;
count++;
flag =1;
}
}
else
{
RESULT[j]=STR[c];
j++;
c++;
m=c;
i=0;
}
}
if(flag == 0)
printf("Pattern does not found\n");
else
{
RESULT[j]='\0';
printf("Pattern found %d times\n",count);
printf("The resultant string is\n%s\n",RESULT);
}
}
Output:
Enter the main string STR :SpiderMan
Enter the pattern string PAT : Spider
Enter the replace string REP : Bat
Pattern found 1 times
The resultant string is
BatMan
WEEK-6
a) Write a C Program to perform various arithmetic operations on pointer variables.
b) Write a C Program to demonstrate the following parameter passing mechanisms:
i) call-by-value ii) call-by-reference
Program:
#include<stdio.h>
#include<conio.h>
void swapv(int,int);
void main()
{
int a,b;
clrscr();
printf("Enter the value of a: ");
scanf("%d",&a);
printf("\nEnter the value of b: ");
scanf("%d",&b);
printf("Before Swapping, FirstNumber=%d SecondNumber=%d",a,b);
swapv(a,b);
getch();
}
Output:
Enter the value of a: 2
Enter the value of b: 4
Program:
#include<stdio.h>
void swapr(int*,int*);
void main()
{
int a,b;
printf("Enter the value of a: ");
scanf("%d",&a);
printf("\nEnter the value of b: ");
scanf("%d",&b);
printf("Before Swapping, FirstNumber=%d SecondNumber=%d",a,b);
swapr(&a,&b);
printf("\n\nAfter Swapping, FirstNumber=%d SecondNumber=%d",a,b);
}
Output:
Enter the value of a: 5
Enter the value of b: 6
#include<stdio.h>
#include<conio.h>
#define MAX 5
int top=-1,s[MAX];
void push();
void pop();
void display();
int main()
{
int choice;
clrscr();
do{
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default:printf("invalid choice entered\n");
break;
}
}while ( choice !=4);
getch();
return 0;
}
void push()
{
intelem;
if(top==MAX-1)
{
printf("\nOverflow");
return;
}
else{
printf("Enter the element to be pushed\n");
scanf("%d",&elem);
top=top+1;
s[top]=elem;
void pop()
{
if(top==-1)
{
printf("Underflow\n");
}
else{
printf("Element popped is %d",s[top]);
top--;
}
}
void display()
{
if(top==-1)
{
printf("Stack is empty\n");
}
else{
for(int i=0;i<=top;i++)
{
printf("%d ",s[i]);
}
}
}
Output:
1.Push an element in a stack
2. Pop an element from stack
3. Display elements from stack
4. Exit
Overflow
#include<stdio.h>
#include<stdlib.h>
struct stack *push(struct stack *, int );
struct stack *push(struct stack *, int);
struct stack *display(struct stack *);
struct stack *pop(struct stack *);
int peek(struct stack *);
struct stack{
int data;
struct stack *next;
};
struct stack *top=NULL;
int main()
{
intval,choice;
do{
printf("\n*****Main Menu*****");
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Peek");
printf("\n4.Display");
printf("\n5.Exit");
printf("\n\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nEnter the number to be pushed onto stack: ");
scanf("%d",&val);
top=push(top,val);
break;
case 2: top=pop(top);
break;
case 3: val=peek(top);
if(val!=-1)
printf("\nThe value at the top of the stack: %d",val);
else
printf("\nStack is empty");
break;
case 4:
top=display(top);
break;
}
//return 0;
}while(choice!=5);
}
struct stack *push(struct stack *top, intval)
{
struct stack *ptr;
ptr=(struct stack *)malloc(sizeof(struct stack));
ptr->data=val;
if(top==NULL)
{
ptr->next=NULL;
top=ptr;
}
else{
ptr->next=top;
top=ptr;
}
return top;
}
while(ptr != NULL)
{
printf("\n %d", ptr -> data);
ptr = ptr -> next;
}
}
return top;
}
int peek(struct stack *top)
{
if(top==NULL)
return -1;
else
return top ->data;
}
Output:
*****Main Menu*****
1.Push
2.Pop
3.Peek
4.Display
5.Exit
*****Main Menu*****
1.Push
2.Pop
3.Peek
4.Display
5.Exit
*****Main Menu*****
1.Push
2.Pop
3.Peek
4.Display
5.Exit
30
20
10
*****Main Menu*****
1.Push
2.Pop
3.Peek
4.Display
5.Exit
20
10
*****Main Menu*****
1.Push
2.Pop
3.Peek
4.Display
5.Exit
STACK UNDERFLOW
*****Main Menu*****
1.Push
2.Pop
3.Peek
4.Display
5.Exit
STACK IS EMPTY
*****Main Menu*****
1.Push
2.Pop
3.Peek
4.Display
5.Exit
int q[4];
int n=4;
int f=0,r=-1;
void insert();
void delete();
void disp();
void main()
{
int choice;
while(1)
{
switch(choice)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: disp();
break;
case 4:exit(0);
default: printf("Invalid choice\n");
break;
}
}
}
void insert()
{
int elem;
if(r==n-1)
printf("Insertion not possible\n");
else if(f==-1 && r==-1)
f=r=0;
else
{
printf("\nEnter the element to be inserted: ");
scanf("%d",&elem);
q[++r]=elem;
}
}
void delete()
{
if(f==-1 || f>r)
printf("Deletion not possible");
else
{
printf("Element deleted is %d",q[f]);
++f;
}
}
void disp()
{
int i;
if(f==-1 || f>r)
{
printf("Queue is empty");
}
else
{
for(i=f;i<=r;i++)
{
printf("%d ",q[i]);
}
}
}
Output:
Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop
Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop
Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop
Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop
Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop
Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop
Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop
Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop
Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop
Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop
Press 1 to insert
Press 2 to delete
Press 3 to display
Press 4 to stop
Result:
Therefore, we have successfully executed the program on Queue using Arrays.
struct linear_queue
{
int info;
struct linear_queue *next;
}*front,*rear,*newnode,*ptr;
void menu();
void display();
int underflow();
void enqueue(int);
void dequeue();
void main()
{
menu();
}
void menu()
{
int choice,item;
printf("\n\nMENU");
printf("\n1. Insert into the queue");
printf("\n2. Delete from queue");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
menu();
break;
case 3:
if(underflow()==1)
{
printf("The queue is:\n");
display();
}
getch();
menu();
break;
case 4:
exit(1);
default:
int underflow()
{
if((front==NULL)&&(rear==NULL))
{
printf("\nQueue is empty");
return(0);
}
else
{
return(1);
}
}
void dequeue()
{
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
{
front=front->next;
}
}
void display()
{
int i;
ptr=front;
i=1;
while(ptr!=NULL)
{
printf("\nNode %d : %d",i,ptr->info);
ptr=ptr->next;
i++;
}
}
Output:
MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 1
Node 1 : 10
MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 1
Node 1 : 10
Node 2 : 20
MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 1
Node 1 : 10
Node 2 : 20
Node 3 : 30
MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 1
Enter the item to be inserted: 40
After inserting queue is:
Node 1 : 10
Node 2 : 20
Node 3 : 30
Node 4 : 40
MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 1
Enter the item to be inserted: 50
After inserting queue is:
Node 1 : 10
Node 2 : 20
Node 3 : 30
Node 4 : 40
Node 5 : 50
MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 3
The queue is:
Node 1 : 10
Node 2 : 20
Node 3 : 30
Node 4 : 40
Node 5 : 50
MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 2
After deletion queue is:
Node 1 : 20
Node 2 : 30
Node 3 : 40
Node 4 : 50
MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 2
After deletion queue is:
Node 1 : 30
Node 2 : 40
Node 3 : 50
MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 2
Node 1 : 40
Node 2 : 50
MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 2
Node 1 : 50
MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 2
Queue is empty
MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 2
Queue is empty
MENU
1. Insert into the queue
2. Delete from queue
3. Display
4. Exit
Enter your choice: 4
WEEK -9
Write a C program that uses Stack operations to perform the following:
i) Converting infix expression into postfix expression
ii) Evaluating the postfix expression
Write a C program that uses Stack operations to Convertinfix expression into postfix
expression.
Aim:- To write a C program to convert infix expression into postfix expression.
Program:-
#include<stdio.h>
#include<stdlib.h>
int stack[20];
int top = -1;
void push(char);
char pop();
int incmprc(char);
int instprc(char);
void IntoPost(char[], char[]);
int main()
{
char infix[20], postfix[20];
printf("Enter the valid infix expression : ");
scanf("%s",infix);
push('#');
IntoPost(infix,postfix);
printf("The corresponding postfix expression is : \n%s\n",postfix);
}
char pop()
{
return stack[top--];
}
Output:
Enter the valid infix expression :a+b*c-(d/e^f)*g*h
The corresponding postfix expression is :
abc*+def^/g*h*-
Result:
Therefore, the conversion of infix expression to postfix expression is successfully executed.
Write a C program that uses Stack operations to evaluating the postfix expression
Aim: To write a C program to evaluate the postfix expression
Program:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
int stack[50];
int top = -1;
void push(int);
int pop();
void push(intnum)
{
stack[++top] = num;
}
int pop()
{
return stack[top--];
}
int main()
{
char suffix[50],ch;
int p1,p2,p3,i;
printf("Enter the valid suffix expression : ");
gets(suffix);
//scanf("%s",suffix);
for(i=0;suffix[i]!='\0';i++)
{
ch = suffix[i];
if(isdigit(ch))
push(ch-'0');//ascii value of ch -ascii value of 0
else
{
p2 = pop();
p1 = pop();
switch(ch)
{
case '+': push(p1+p2);
//printf("%d",pop());
break;
case '-': push(p1-p2);
break;
case '*': push(p1*p2);
break;
case '/': if(p2 == 0)
{
printf("Divide by Zero error\n");
exit(0);
}
push(p1/p2);
break;
case '%': if(p2 == 0)
{
printf("Divide by Zero error\n");
exit(0);
}
push(p1%p2);
break;
case '^': push(pow(p1,p2));
break;
default :printf("Invalid Expression\n");
}
}
}
printf("The value of given suffix expression is : %d\n",pop());
}
Output:
Enter the valid suffix expression : 93/1-24^+91*2%-
The value of given suffix expression is : 17
WEEK -10
Write a C program that uses functions to perform the following operations on Singly Linked
list. i)Creation ii) Insertion iii) Deletion iv) Traversal
if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("\nNode inserted\n");
}
}
void insertion_last()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value");
scanf("%d",&item);
ptr->data=item;
if(head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else
{
temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
}
}
printf("\nnode inserted\n");
}
void insertion_specified()
{
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
temp=head;
printf("Enter the location");
scanf("%d",&loc);
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\n There are less than %d elements", loc);
return;
}
}
printf("Enter value");
scanf("%d",&item);
ptr->data = item;
ptr->next = temp->next;
ptr -> prev = temp;
temp->next = ptr;
temp->next->prev=ptr;
printf("\nnode inserted\n");
}
}
void deletion_beginning()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_last()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_specified()
{
struct node *ptr, *temp;
int val;
printf("\n Enter the data after which the node is to be deleted : ");
scanf("%d", &val);
ptr = head;
while(ptr -> data != val)
ptr = ptr -> next;
if(ptr -> next == NULL)
{
printf("\nCan't delete\n");
}
else if(ptr -> next -> next == NULL)
{
ptr ->next = NULL;
}
else
{
temp = ptr -> next;
ptr -> next = temp -> next;
temp -> next -> prev = ptr;
free(temp);
printf("\nnode deleted\n");
}
}
void display()
{
struct node *ptr;
printf("\n printing values...\n");
ptr = head;
while(ptr != NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("\nitem found at location %d ",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("\nItem not found\n");
}
}
OUTPUT:-
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
Enter your choice?
8
printing values...
20
10
50
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Insert at any random location
4.Delete from Beginning
5.Delete from last
6.Delete the node after the given data
7.Search
8.Show
9.Exit
Enter your choice?
9
WEEK -12
Write a C program that uses functions to perform the following operations on circular
linked list. i) Creation ii) Insertion iii) Deletion iv) Traversal
Aim: To write a C program to implement Circular Linked List
Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
void beginsert ();
void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 7)
{
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from Beginning\n4.Delete from
last\n5.Search for an element\n6.Show\n7.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
begin_delete();
break;
case 4:
last_delete();
break;
case 5:
search();
break;
case 6:
display();
break;
case 7:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void beginsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter the node data?");
scanf("%d",&item);
ptr -> data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp->next != head)
temp = temp->next;
ptr->next = head;
temp -> next = ptr;
head = ptr;
}
printf("\nnode inserted\n");
}
}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
printf("\nEnter Data?");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp -> next != head)
{
temp = temp -> next;
}
temp -> next = ptr;
ptr -> next = head;
}
printf("\nnode inserted\n");
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nUNDERFLOW");
}
else if(head->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{ ptr = head;
while(ptr -> next != head)
ptr = ptr -> next;
ptr->next = head->next;
free(head);
head = ptr->next;
printf("\nnode deleted\n");
}
}
void last_delete()
{
struct node *ptr, *preptr;
if(head==NULL)
{
printf("\nUNDERFLOW");
}
else if (head ->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
while(ptr ->next != head)
{
preptr=ptr;
ptr = ptr->next;
}
preptr->next = ptr -> next;
free(ptr);
printf("\nnode deleted\n");
}
}
void search()
{
struct node *ptr;
int item,i=0,flag=1;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
if(head ->data == item)
{
printf("item found at location %d",i+1);
flag=0;
}
else
{
while (ptr->next != head)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
}
if(flag != 0)
{
printf("Item not found\n");
}
}
}
void display()
{
struct node *ptr;
ptr=head;
if(head == NULL)
{
printf("\nnothing to print");
}
else
{
printf("\n printing values ... \n");
while(ptr -> next != head)
{
printf("%d\n", ptr -> data);
ptr = ptr -> next;
}
printf("%d\n", ptr -> data);
}
}
Output:
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit
Result:
Therefore, the implementation of Circular Linked List is successfully executed.
WEEK-13
Write a C program that uses functions to perform the following:
i) Creating a Binary Tree of integers
ii) Traversing the above binary tree in preorder, inorder and postorder.
Aim:- To write a C program to implement a Binary Tree and traverse the binary tree.
Program:
#include<stdio.h>
#include <stdlib.h>
#define MAX 20
struct node
{
int data;
struct node *lchild, *rchild;
};
typedefstruct node *NODE;
NODE tree = NULL;
voidCreateBST (int a[MAX], int n)
{
NODE temp, p, q;
int i;
for(i=0;i<n;i++)
{
temp =(struct node *)malloc(sizeof(struct node*));
temp->data = a[i];
temp->lchild = temp->rchild = NULL;
if(tree == NULL)
tree = temp;
else
{
p = q = tree;
while(q != NULL)
{
p=q;
if(a[i] < p->data)
q = p->lchild;
else if(a[i] > p->data)
q = p->rchild;
else
{
free(temp);
break;
}
}
if( q == NULL)
{
if(a[i] < p->data)
p->lchild = temp;
else
p->rchild = temp;
}
}
}
printf("Binary Search Tree created\n\n");
}
voidInorder(NODE tree)
{
if(tree != NULL)
{
Inorder(tree->lchild);
printf("%d ",tree->data);
Inorder(tree->rchild);
}
}
voidPreorder(NODE tree)
{
if(tree != NULL)
{
printf("%d ",tree->data);
Preorder(tree->lchild);
Preorder(tree->rchild);
}
}
voidPostorder(NODE tree)
{
if(tree != NULL)
{
Postorder(tree->lchild);
Postorder(tree->rchild);
printf("%d ",tree->data);
}
}
int main()
{
int a[MAX], n, i, choice;
while(1)
{
printf("\n\n**********************MENU*****************");
printf("\n1. Create a BST of n integers\n2. Traverse the BST in Inorder\n3. Traverse
the BST in Preorder\n4. Traverse the BST in Postorder\n5. Exit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("Enter the number of integers : ");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
CreateBST(a, n);
break;
case 2 : printf("Inorder Traversal :\n");
Inorder(tree);
break;
case 3 : printf("Preorder Traversal :\n");
Preorder(tree);
break;
case 4 : printf("Postoder Traversal :\n");
Postorder(tree);
break;
case 5 : exit(0);
break;
}
}
}
Output:-
**********************MENU*****************
1. Create a BST of n integers
2. Traverse the BST in Inorder
3. Traverse the BST in Preorder
4. Traverse the BST in Postorder
5. Exit
Enter your choice : 1
Enter the number of integers : 12
Enter the elements
6
9
5
2
8
15
24
14
7
8
5
2
Binary Search Tree created
**********************MENU*****************
1. Create a BST of n integers
2. Traverse the BST in Inorder
3. Traverse the BST in Preorder
4. Traverse the BST in Postorder
5. Exit
Enter your choice : 2
InorderTraversal :
2 5 6 7 8 9 14 15 24
**********************MENU*****************
1. Create a BST of n integers
2. Traverse the BST in Inorder
3. Traverse the BST in Preorder
4. Traverse the BST in Postorder
5. Exit
Enter your choice : 3
PreorderTraversal :
6 5 2 9 8 7 15 14 24
**********************MENU*****************
1. Create a BST of n integers
2. Traverse the BST in Inorder
3. Traverse the BST in Preorder
4. Traverse the BST in Postorder
5. Exit
Enter your choice : 4
PostoderTraversal :
2 5 7 8 14 24 15 9 6
**********************MENU*****************
1. Create a BST of n integers
2. Traverse the BST in Inorder
3. Traverse the BST in Preorder
4. Traverse the BST in Postorder
5. Exit
Enter your choice : 5
Result:-
Therefore the binary tree program was successfully executed.
WEEK-14
i) Linear search
ALGORITHM :
LINEAR_SEARCH(A, N, VAL)
Step 1: [INITIALIZE] SET POS = -1
Step 2: [INITIALIZE] SET I = 1
Step 3: Repeat Step 4 while I<=N
Step 4: IF A[I] = VAL
SET POS = I
PRINT POS
Go to Step 6
[END OF IF]
SET I = I + 1
[END OF LOOP]
Step 5: IF POS = –1
"PRINT VALUE IS NOT PRESENT IN THE ARRAY"
[END OF IF]
Step 6: EXIT
Write a program to search an element in an array using the linear search technique.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define size 20 // Added so the size of the array can be altered more easily
int main()
{
int arr[size], num, i, n, found = 0, pos = -1;
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
printf("\n Enter the number that has to be searched : ");
scanf("%d", &num);
for(i=0;i<n;i++)
{
if(arr[i] == num)
{
found =1;
pos=i;
printf("\n %d is found in the array at position= %d", num,pos+1);
/* +1 added in line 23 so that it would display the number in
the first place in the array as in position 1 instead of 0 */
break;
}
}
if (found == 0)
printf("\n %d does not exist in the array", num);
return 0;
}
OUTPUT:
Enter the number of elements in the array : 4
Enter the elements: 10 15 72 20
Enter the number that has to be searched :72
72 is found in the array at position= 3
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define size 10 // Added to make changing size of array easier
int smallest(int arr[], int k, int n); // Added to sort array
void selection_sort(int arr[], int n); // Added to sort array
int main()
{
int arr[size], num, i, n, beg, end, mid, found=0;
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
printf("\n Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
selection_sort(arr, n); // Added to sort the array
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
printf("\n\n Enter the number that has to be searched: ");
scanf("%d", &num);
beg = 0, end = n-1;
while(beg<=end)
{
mid = (beg + end)/2;
if (arr[mid] == num)
{
printf("\n %d is present in the array at position %d", num, mid+1);
found =1;
break;
}
else if (arr[mid]>num)
end = mid-1;
else
beg = mid+1;
}
if (beg > end && found == 0)
printf("\n %d does not exist in the array", num);
return 0;
}
OUTPUT :
Enter the number of elements in the array:4
Enter the elements: 10 15 72 20
The sorted array is: 10 15 20 72
Enter the number that has to be searched:20
20 is present in the array at position 3
WEEK-15
i) Bubble sort
ALGORITH:
BUBBLE_SORT(A, N)
Step 1: Repeat Step 2 For I = 0 to N-1
Step 2: Repeat For J = 0 to N - I
Step 3: IF A[J] > A[J + 1]
SWAP A[J] and A[J+1]
[END OF INNER LOOP]
[END OF OUTER LOOP]
Step 4: EXIT
Write a program to enter n numbers in an array. Redisplay the array with elements being
sorted in ascending order.
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, temp, j, arr[10];
clrscr();
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr [i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n–i–1;j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("\n The array sorted in ascending order is :\n");
for(i=0;i<n;i++)
printf("%d\t", arr[i]);
getch();
return 0;
}
Output
Enter the number of elements in the array : 10
Enter the elements : 8 9 6 7 5 4 2 3 1 10
The array sorted in ascending order is :
1 2 3 4 5 6 7 8 9 10
SELECTION SORT(ARR, N)
Step 1: Repeat Steps 2 and 3 for K = 1 to N-1
Step 2: CALL SMALLEST(ARR, K, N, POS)
Step 3: SWAP A[K] with ARR[POS]
[END OF LOOP]
Step 4: EXIT
selection_sort(arr, n);
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
}
int smallest(int arr[], int k, int n)
{
int pos = k, small=arr[k], i;
for(i=k+1;i<n;i++)
{
if(arr[i]< small)
{
small = arr[i];
pos = i;
}
}
return pos;
}
void selection_sort(int arr[],int n)
{
int k, pos, temp;
for(k=0;k<n;k++)
{
pos = smallest(arr, k, n);
temp = arr[k];
arr[k] = arr[pos];
arr[pos] = temp;
}
}
Week 15 iii) Insertion sort
ALGORITHM :
INSERTION-SORT (ARR, N)
Step 1: Repeat Steps 2 to 5 for K = 1 to N – 1
Step 2: SET TEMP = ARR[K]
Step 3: SET J = K - 1
Step 4: Repeat while TEMP <= ARR[J]
SET ARR[J + 1] = ARR[J]
SET J = J - 1
[END OF INNER LOOP]
Step 5: SET ARR[J + 1] = TEMP
[END OF LOOP]
Step 6: EXIT
typedefstruct NODE
{
intreg_no;
int age;
char name[20];
struct NODE *next;
} node;
node* deq();
void create();
int reserve(node*);
int cancel(int);
void enq(node*);
void display();
node *start;
node *front;
node *rear;
int count=0;
intnum=0;
void create( )
{
node *new;
new=(node *)malloc(sizeof(node));
new->reg_no=1;
printf("Name: ");
scanf("%s", new->name);
printf("Age : ");
scanf("%d", &new->age);
start=new;
new->next=NULL;
num++;
}
int reserve(node *start)
{
if(start==NULL)
{
create(start);
return 1;
}
else
{
new_node=(node *)malloc(sizeof(node));
printf("Name: ");
scanf("%s", new_node->name);
printf("Age : ");
scanf("%d", &new_node->age);
new_node->next=NULL;
if(num<=size)
{
num++;
new_node->reg_no=num;
temp->next=new_node;
return 1;
}
else
{
enq(new_node);
return 0;
}
}
}
int cancel(intreg)
{
node *ptr, *preptr, *new;
ptr=start;
preptr=NULL;
if(start==NULL)
return -1;
if(ptr->next==NULL &&ptr->reg_no==reg)
{
start=NULL;
num--;
free(ptr);
return 1;
else{
while(ptr->reg_no!=reg&&ptr->next!=NULL)
{
preptr=ptr;
ptr=ptr->next;
}
if(ptr==NULL &&ptr->reg_no!=reg)
return -1;
else
preptr->next=ptr->next;
free(ptr);
new=deq();
while(preptr->next!=NULL)
preptr=preptr->next;
preptr->next=new;
num--;
return 1;
}
}
node* deq()
{
node *front1;
front1=front;
if(front==NULL)
return NULL;
else
{
count-- ;
if(front->next!=NULL)
{
front=front->next;
front1->next=NULL;
return front1;
}
else
{
front=NULL;
rear=NULL;
return front1;
}
}
void display()
{
node *temp;
temp=start;
while(temp!=NULL)
{
printf("\nRegistration Number: %d\n", temp->reg_no);
printf("Name : %s\n\n", temp->name);
temp=temp->next;
}
int main()
{
int choice, status=0,canc=0, reg=0;
start=NULL;
rear=NULL;
front=NULL;
printf("\t\t\t***RAILWAY RESERVATION***\t\t\t\t\n");
intch =0;
while(ch!=4)
{
printf("\n\nDo you want to - \n 1. Reserve a ticket? \n 2.Cancel Booking \n 3. Display
passenger details \n 4. exit\n\n");
scanf("%d", &choice);
switch(choice)
{
case 1 : status=reserve(start);
if(status==0)
printf("\nBooking Full!! \nYouare added to waiting list. Waiting list number %d", count);
else
printf(" \nBooking Successful!!! Enjoy your journey! Your Reg No is %d\n\n", num);
break;
case 3: display();
break;
case 4: exit(0);
break;
default: printf("\nWrong choice!\n");