Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 58

CONDITIONAL STATEMENTS

EX.NO: 1A
DATE :
1A) ODD OR EVEN

AIM :

To write a c program to check whether given Number is odd or even.

ALGORITHM :

Step 1: Declare a variable to get a Number


Step 2: Read the input
Step 3: Get the remainder of given number using modulo operator
Step 4: If remainder is 0 prints “Even Number”, else print “Odd Number”.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,rem;
clrscr();
printf("Enter a Number\n");
scanf("%d",&a);
rem=a%2;
if(rem==0)
printf("The Given Number is Even");
else
printf("The Given Number is Odd");
getch();
}

OUTPUT:

Enter a Number
13
The Given Number is Odd

RESULT:

Thus the c program to check whether given Number is odd or even was
written, entered, executed and the output was verified.

1
EX.NO: 1B
DATE :
1B) BIGGEST OF 3 NUMBERS

AIM :

To write a c program to examine the biggest of given three numbers.

ALGORITHM :

Step 1: Declare three integer variables


Step 2: Read the 3 inputs
Step 3: Compare first two numbers and go to Step4
Step 4: If first number is greater than second number then compare first
number with third number else go to step 6
Step 5: If first number is greater than third number print first number as
biggest number else print third number as biggest
Step 6: Compare second number with third number
Step 7: If second number is greater than third number print second number
as biggest number else print third number as biggest

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter 3 Numbers\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("The First Number %d(a) is Biggest\n",a);
}
}
else if(b>c)
{
printf("The Second Number %d(b) is Biggest\n",b);
}
else
printf("The Third Number %d(c) is Biggest\n",c);
}

2
OUTPUT:

Enter 3 Numbers
5
9
2
The Second Number 9(b) is Biggest

RESULT:

Thus the c program to examine the biggest of given three numbers was
Written, entered, executed and the output was verified.
3
EX.NO: 1C
DATE :
1C) ARITHMETIC CALCULATOR
AIM :

To write a c program to implement an Arithmetic Calculator.

ALGORITHM :

Step 1: Get a number from the user


Step 2: Enter the choice
Step 3: Pass the choice into switch case
Step 4: In case 1,
a. Copy the given number into a variable
b. Initialize a counter to 1 and sum to 0
c. Extract the remainder of given number while dividing 10
d. Multiply the sum by 10
e. Overwrite the sum by adding above remainder with
f. available sum
g. Overwrite the number by divide with 10
h. Repeat the steps a to f until the number is greater than 0
i. Compare the sum and copy of the number
j. If they are equal print as “Palindrome” else print “Not
k. Palindrome”

Step 5: In case 2,
a. Copy the given number into a variable
b. Initialize a counter to 1 and sum to 0
c. Extract the remainder of given number while dividing 10
d. Calculate the value of remainder by assigning power 3
e. Overwrite the sum by adding above result with available sum
f. Overwrite the number by divide with 10
g. Repeat the steps a to e until the number is greater than 0
h. Compare the sum and copy of the number
i. If they are equal print as “Armstrong” else print “Not
j. Armstrong”

Step 6: In case 3,
a. Initialize a flag value with 5
b. Initialize a counter to 2
c. Extract the remainder of given number by dividing with
d. counter value
e. If the remainder is 0 changes the flag value to 0 and go to
f. step g else go to next step.
g. Increment the counter value by 1
h. Repeat the steps a to e until counter is less than or equal to
i. square root of the given number
j. Check the flag value
k. If flag value is 0 then print as “Prime Number” else print as

4
PROGRAM:

#include<stdio.h>
#include<math.h>
main()
{
int a,i,sum=0,n,ch,m;
printf("\nEnter a Number\n");
scanf("%d",&a);
printf("\n1.Palindrome\n2.Armstrong\n3.Prime\n");
printf("\nEnter the Choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
n=a;
while(a>0)
{
i=a%10;
sum=(sum*10)+i;
a=a/10;
}
if(n==sum)
printf("Given Number is Palindrome\n");
else
printf("Given Number is Not Palindrome\n");
break;
case 2:
n=a;
do
{
i=a%10;
sum=sum+(i*i*i);
a=a/10;
}
while(a>0);
if(n==sum)
printf("Given Number is Armstrong\n");
else
printf("Given Number is Not Armstrong\n");
break;
case 3:
m=5;
n=sqrt(a);
for(i=2;i<=n;i++)
{
if(a%i==0)
{
m=0;
break;
}
5
}
if(m==0)
printf("Given Number is Prime\n");
else
printf("Given Number is Not Prime\n");
break;
}
}

OUTPUT:

Enter a Number
121
1.Palindrome
2.Armstrong
3.Prime

Enter the Choice:


1
Given Number is Palindrome

RESULT:

Thus the menu driven c program to check whether the given number is
Palindrome, Armstrong and Prime was written, entered, executed and the output
was verified.
6
EX.NO: 1D
DATE :
1D) SUM OF DIGITS

AIM :

To write a c program to find the sum of digits for a given number.

ALGORITHM :

Step 1: Declare a integer variable and initialize the sum as 0


Step 2: Read the input
Step 3: Take the remainder of given number while dividing 10
Step 4: Overwrite the sum by adding above remainder with available sum
Step 5: Overwrite the number by divide with 10
Step 6: Repeat the steps 3 to 5 until the number is greater than 0
Step 7: Print the sum

PROGRAM :

#include<stdio.h>
#include<conio.h>
main()
{
int n,i,sum=0;
printf("Enter a Number\n");
scanf("%d",&n);
do
{
i=n%10;
sum=sum+i;
n=n/10;
}
while(n>0);
printf("The Sum of digit is %d\n",sum);
getch();
}

OUTPUT:

Enter a Number
5891
The Sum of digit is 23

RESULT:

Thus the c program to find the sum of digits for a given number was written,
entered, executed and the output was verified.

7
EX.NO: 1E
DATE :
1E) FACTORIAL
AIM :

To write a c program to find the sum of digits for a given number.

ALGORITHM :

Step 1. Start
Step 2. Read the number n
Step 3. [initialize] i=1, fact=1
Step 4. Repeat step 4 through 6 until i=n
Step 5. Fact=fact*i
Step 6. I=i+1
Step 7. Print fact
Step 8. Stop.

PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,fact=1;
clrscr();
printf("Enter any number : ");
scanf("%d", &n);
for(i=1; i<=n; i++)
fact = fact * i;
printf("Factorial value of %d = %d",n,fact);
return 0;
}

OUTPUT:

Enter any number: 7


Factorial value of 7 = 5040

RESULT:

Thus the c program to find the Factorial for a given number was written,
Entered, executed and the output was verified.
8
SIMPLE C PROGRAM USING ARRAY

EX.NO: 2A
DATE :
2A) DISPLAY EVEN NUMBERS OF AN ARRAY
AIM :

To write a c program to print the even numbers of given array elements.

ALGORITHM :

Step 1: Declare an array with necessary size


Step 2: Get the value for total number of elements
Step 3: Initialize an index value to 0
Step 4: Read the input
Step 5: Increment the index value by 1
Step 6: Repeat steps 4 & 5 until counter less than total no. of elements
Step 7: Initialize an index value to 0
Step 8: Extract the remainder by dividing array index value with 2
Step 9: If the remainder is 0 print the value
Step 10: Increment the index value by 1
Step 11: Repeat steps 8 to 10 until index value less than total no. of elements

PROGRAM:
#include<stdio.h>
main()
{
int i,n,a[10];
printf("Enter total no. of Elements\n");
scanf("%d",&n);
printf("Enter Array elements one by one\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("The even numbers of given array:\n");
for(i=0;i<n;i++)
{
if(a[i]%2==0)
printf("%d\n",a[i]);
}
}

9
OUTPUT:
Enter total no. of Elements
6
Enter Array elements one by one
98
11
35
61
22
14
The even numbers of given array:
98
22
14

RESULT:

Thus the menu driven c program to print the even numbers of given array

10
elements was written, entered, executed and the output was verified.
EX.NO: 2B
DATE :
2B) STRING CONCATENATION
AIM :
To write a c program to find the length of given two strings and concatenate them.

ALGORITHM :

Step 1: Create two character arrays with necessary size


Step 2: Read the Strings
Step 3: Calculate the string lengths using strlen function
Step 4: Print the string lengths
Step 5: Join the two strings using strcat function
Step 6: Print the concatenated string

PROGRAM :
#include<stdio.h>
#include<string.h>
main()
{
char s[20],s1[20];
printf("Enter a String1\n");
scanf("%s",s);
printf("Enter a String2\n");
scanf("%s",s1);
strcat(s,s1);
printf("The Concatenated String is %s\n",s);
}

OUTPUT:
Enter a String1
hai
Enter a String2
hello
The Concatenated String is haihello

RESULT:

Thus the c program to find the length of given two strings and concatenate
them was written, entered, executed and the output was verified.
11
EX.NO: 2C
DATE :
2C) SWAP TWO NUMBERS
AIM :

To write a c program to swap two integer numbers using pointers.

ALGORITHM :

Step 1: Start the program


Step 2: Read the value of num1,num2
Step 3: Address of num1 & num2 is passed to swap function
Step 4: pointers *a and *b accept those values
Step 5: The pointer a and b points to address of num1 and num2
Step 6: The value of pointer are changed, the value in memory location also changed
Step 7: Change made to *a and *b was reflected in num1 and num2 in main function.

PROGRAM:

#include <stdio.h>
#include <conio.h>
void swap(int *a,int *b);
int main()
{
int num1,num2;
clrscr();
printf(“Enter the 2 numbers to swap\n”);
scanf(“%d%d”,&num1,&num2);
swap(&num1,&num2);
printf("Number1 = %d\n",num1);
printf("Number2 = %d",num2);
return 0;
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}

12
OUTPUT:

Enter the 2 numbers to swap


5
10
Number1 = 10
Number2 = 5

RESULT:

13
Thus the c program to swap two numbers using pointer was entered, executed and the
output was verified.
EX.NO: 2D
DATE :

2D) Prime Numbers Between two Intervals by Making User-defined Function

AIM :

To write a c program to print the Prime Numbers Between two Intervals by Making User-defined
Function.

ALGORITHM :
Step 1: Start the program
Step 2: Read the value of two positive integers
Step 3: Display all prime numbers between the intervals
Step 4: Create User-Defined function to check the prime number
Step 5: The value of prime numbers passed to the function using loop.
Step 6: The Function checks whether a number is prime or not.
Step 7: If the number is prime it returns to 1, if not it return to 0.

PROGRAM:
#include<stdio.h>
#include<conio.h>
int check_prime(int num);
int main()
{
int n1,n2,i,flag;
clrscr();
printf("Enter two numbers(intervals): ");
scanf("%d %d",&n1, &n2);
printf("Prime numbers between %d and %d are: ", n1, n2);
for(i=n1+1;i<n2;++i)
{
flag=check_prime(i);
if(flag==0)
printf("%d ",i);
}
return 0;
}
int check_prime(int num) /* User-defined function to check prime number*/
{
int j,flag=0;
for(j=2;j<=num/2;++j)
{
if(num%j==0)
{
flag=1;
break;
}
}
14
return flag;
}
OUTPUT:
Enter two numbers(intervals):
10
30
Prime numbers between 10 and 30 are: 11 13 17 19 23 29

RESULT:

15
Thus the c program to print the Prime Numbers Between two Intervals by making user
defined functions was entered, executed and the output was verified.
Singly linked list
EX.NO: 3A
DATE :

AIM : To implement singly linked list.

ALGORITHM :

Step 1: Start the program.


Step 2: Declare the necessary variables and functions.
Step 3: Call the variable and functions in singly linked list.
Step 4: Read the choice.
Step 5: If choice is 1, perform create list function.
Step 6: If choice is 2, perform display function.
Step 7: If choice is 3,perform insert function.
Step 8: If choice is 4,perform insert first function.
Step 9: If choice is 5, perform search function.
Step10: If choice is 6, perform delete function.
Step11: If choice is 7, perform exit operation.
Step12: Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct list
{
int data;
struct list*next;
}node;
node *newnode,*temp,*head;
node* getnode()
{
node *n;
n=(node*)malloc(sizeof(node));
n->next=NULL;
return(n);
}
void create()
{
char ch=’y';
head->next=NULL;
do
{
newnode =getnode();
printf(“\n enter the element:”);

16
scanf(“%d”,&newnode->data);
if(head->next==NULL)
head->next=newnode;
else
temp->next=newnode;
temp=newnode;
printf(“do u wnt to cont?:”);
ch=getch();
}
while(ch==’y'||ch==’Y');
}
void display()
{
printf(“\n\nthe list is:”);
for(temp=head->next;temp!=NULL;temp=temp->next)
{
printf(“\t%d”,temp->data);
}
getch();
}
void insert()
{
int pos;
temp=head->next;
newnode=getnode();
printf(“\n\nenter the new element & pos element aft to be inserted:”);
scanf(“%d%d”,&newnode->data,&pos);
while(temp->data!=pos&&temp->next!=NULL)
{
temp=temp->next;
}
newnode->next=temp->next;
temp->next=newnode;
printf(“\nnew node inserted…”);
display();
}
void first()
{
newnode=getnode();
printf(“\nenter the new element:”);
scanf(“%d”,&newnode->data);
newnode->next=head->next;
head->next=newnode;
printf(“\nnew node inserted at first…”);
display();
}
void del()
{
int pos;
node *temp1;
temp=head->next;
17
printf(“\n enter the pos element:”);
scanf(“%d”,&pos);
temp1=head;
while(temp->data!=pos&&temp->next!=NULL)
{
temp1=temp;
temp=temp->next;
}
temp1->next=temp->next;
free(temp);
printf(“\n\nnode deleted…”);
display();
}
void search()
{
int pos,i=1;temp=head->next;
printf(“enter the element to be search”);
scanf(“%d”,&pos);
while(temp->next!=NULL&&temp->data!=i)
{
if(temp->data==pos)
{
printf(“\n element at pos %d in the list”,i);
;
break;
}
else
{
i+=1;
temp=temp->next;
}
}
display();
}
void main()
{
int op;
do
{
clrscr();
printf(“\n\n\t\tLinked list implementation of list”);
printf(“\n1.Create\n2.Display\n3.Insert\n4.Insert first\n5.Search\n6.Delete\n7.Exit”);
printf(“\n Enter the option:”);
scanf(“%d”,&op);
switch(op)
{
case 1:
create();
break;
case 2:
display();
18
break;
case 3:
insert();
break;
case 4:
first();
break;
case 5:
search();
break;
case 6:
del();
break;
}
}
while(op<=6);
}

OUTPUT:

LINKED LIST IMPLEMENTATION OF LIST

1.Create
2.Display
3.Insert
4.Insertfirst
5.Search
6.Delete
7.Exit

Enter the Option : 1


Enter the element :11
Do you want to Continue :Y
Enter the Element : 22
Do you want to Continue :Y
Enter the Element : 33
Do you want to Continue :Y
Enter the Element : 44
Do you want to Continue :n

Enter the Option: 2


The List is 11 22 33 44

Enter the Option : 3


Enter the new element & pos element aft to be inserted: 99 22
11 22 99 33 44
New node inserted.

Enter the Option :4


Enter the new element: 55
55 11 22 99 33 44
19
New node inserted at first…

Enter the Option : 5


Enter the element to be search : 99
Element at pos 4 in the list..

Enter the Option :6


Enter the pos element : 11
Node deleted…

Enter the Option : 7

20
Result :
Thus the program is implemented and verified.

21
Doubly linked list
EX.NO : 3B
DATE :

AIM : To implement doubly linked list.

ALGORITHM :

Step 1: Start the program.


Step 2: Declare the necessary variables and functions.
Step 3: Call the variable and functions in doubly linked list.
Step 4: Read the choice.
Step 5: If choice is 1,perform create list function.
Step 6: If choice is 2,perform display function.
Step 7: If choice is 3,perform insert function.
Step 8: If choice is 4,perform delete function.
Step 9:If choice is 5,perform exit operation.
Step10:Stop the program.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct list
{
int data;
struct list*next,*pre;
}node;
node *newnode,*temp,*head=NULL;
node* getnode()
{
node *n;
n=(node*)malloc(sizeof(node));
n->next=NULL;
n->pre=NULL;
return(n);
}
void create()
{
char ch='y';
head->next=NULL;
do
{
newnode =getnode();
printf("\n Enter the element:");
scanf("%d",&newnode->data);
if(head->next==NULL)
{
head->next=newnode;
newnode->pre=head;

22
} else
{ temp->next=newnode;
newnode->pre=temp;
} temp=newnode;
printf("do u wnt to cont?:");
ch=getch();
} while(ch=='y'||ch=='Y');
}
void display()
{
printf("\n\nthe list is:");
for(temp=head->next;temp!=NULL;temp=temp->next)
{
printf("\t%d",temp->data);
} getch();
}
void insert()
{
int pos;
temp=head->next;
newnode=getnode();
printf("\n\nenter the new element & pos element aft to be inserted:");
scanf("%d%d",&newnode->data,&pos);
while(temp->data!=pos&&temp->next!=NULL)
{
temp=temp->next;
}
newnode->next=temp->next;
newnode->pre=temp;
temp->pre=newnode;
newnode->pre->next=newnode;
printf("\nnew node inserted");
display();
}
void del()
{
int pos;
node *temp1;
temp=head;
printf("\n enter the pos element:");
scanf("%d",&pos);
while(temp->data!=pos&&temp->next!=NULL)
{
temp1=temp;
temp=temp->next;
}
temp1->next=temp->next;
free(temp);
printf("\n\nnode deleted");
display();
}
23
void main()
{
int op;
head=(node*)malloc(sizeof(node));
do
{
clrscr();
printf("\n\n\t\tLinked list implementation of list");
printf("\n1.create\n2.display\n3.insert\n4.delete\n5.exit");
printf("\n enter the option:");
scanf("%d",&op);
switch(op)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert();
break;
case 4:
del();
break;
}
}
while(op<=4);
}

24
OUTPUT:

Linked list implementation of list


1.create
2.display
3.insert
4.delete
5.exit
enter the option:1

Enter the element:10


do u wnt to cont?:
Enter the element:20
do u wnt to cont?:
Enter the element:30
do u wnt to cont?:
Enter the element:40
do u wnt to cont?: n

enter the option:2


the list is: 10 20 30 40

enter the option:3


enter the new element & pos element aft to be inserted:99 30

new node inserted


the list is: 10 20 30 99 40

enter the option:4


enter the pos element:20

node deleted
the list is: 10 30 99 40

enter the option:5


Null pointer assignment

RESULT:
Thus the C program to implement doubly linked list is executed.
25
File Handling Concepts
EX.NO : 4A
DATE :

AIM :

Program To write data file and read data from file

ALGORITHM :
step1: start
step2: take a character ch and define a file pointer f2
step3: open a file data.dat for writing
step4: while ((ch=getch()!=eof)
read a character ch
step5: close the file data.dat
step6: open the same file for reading
while((ch=get(f2)!=EOF)
display charecter on monitor
step7: close the data.dat
step8:stop

A program to write data file and read data from file


#include<stdio.h>
main()
{
charch;
FILE *f2;
f2=fopen("data.dat","w");
while((ch=getchar())!=EOF)
putc(ch,f2);
fclose(f2);
f2=fopen("data.dat","r");
while((ch=getc(f2))!=EOF)
putchar(ch);
fclose(f2);
}

26
OUTPUT:
Saraswathi Velu College of Engineering.
Saraswathi Velu College of Engineering.

Result:

Thus the C program to write data file and read data from file is executed.
27
EX.NO : 4B
DATE :

AIM : Program to write integer data into file and read it from file

ALGORITHM :
step1: start
step2: take a number and define a file pointer
step3: open a file data.dat for writing
step4: read on integer and also read an integer into file
step5: close the file data.dat
step6: open the same file for reading
display an integer
step7: stop

A program to write integer data into file and read it from file
#include<stdio.h>
main()
{
int num;
FILE *f2;
f2=fopen("data.int","w");
scanf("%d",&num);
putw(num,f2);
fclose(f2);
f2=fopen("data.int","r");
num=getw(f2);
printf("%d",num);
fclose(f2);
}

OUTPUT:
12
12

Result:

Thus output of the Program to write integer data into file and read it from file is executed.
28
EX.NO : 4C
DATE :

AIM : Program to write product details

ALGORITHM :
step1: start
step2: take a character array c
step3: take three integers p,q,b
step4: define a file pointer fp
step5: open a file data.dat for writing
step6: accept c from user and p,q
step7: write string in c and values of p,q into file
step8: close the file data.dat
step9: open the same file for reading
step10: evaluate p*q and store in b display c,p,q,b to the user
step11: close the data.dat
step12: stop
A program to write product details
#include<stdio.h>
main()
{
char c[20];
int p,q,b;
FILE *f2;
f2=fopen("data.dat","w");
printf("enter item name,price,quality");
scanf("%s%d%d",&c,&p,&q);
b=p*q;
printf("%s%d%d%d",c,p,q,b);
fclose(f2);
}

29
Output:
Enter item name, price, and quality
Rice 25 1
Rice 25 1 25

Result:

Thus output of the program to write product details is executed.


30
Stack Implementation
EX.NO : 5A
DATE :

AIM: Program to implement Stack operations using arrays

ALGORITHM:
1. push(s,top,x):
step1: start
step2:(check for stack overflow)
if(top>=max)
display "stack overflow"
return
step3:[increment top pointer]
top++
step4:[increment an element in thestack]
s[top] <- x
step5:[finished]
return
2.pop(s,top)
step1:(check for stack underflow)
if(top==0)
display() "stack underflow"
step2:[decrement top operator]
top<- top-1
step3:[delete an element from the stack]
return
(s[top+1])

31
PROGRAM: Stack operations using arrays
#include<stdio.h>
#define max 10
void push();
void pop();
void display();
int s[max];
int top=0;
void main()
{
char ch;
int choice;
do
{
printf("enter choice of operation");
printf("1.push(),2.pop(),3.display()");
scanf("%d",&choice);
switch(choice)
{
case1:
push();
break;
case2:
pop();
break;
case3:
display();
break;
default:
printf("invalid option");
}
printf("do u wantto continue y/n");
fflush(stdin);
scanf("%c",&ch);
}
while(ch=='y'||ch=='y')

32
}
void push()
{
int item;
if(top>=max)
printf("stackisfull");
else
{
printf("enter any item");
scanf("%d",&item);
top++;
s[top]=item;
}
}
void pop()
{
int item;
if(top==0)
printf("stack is empty");
else
{
item=s[top];
printf("the related elemnt is %d",item);
top--;
}
}
void display()
{
int item;
int i;
if(top==0)
printf("\n stack is empty no element isdisplayed");
else
{
printf("\n%d\n",s[i]);
printf("\n----\n");
}

33
}

34
Output:
enter choice of operation1.push(),2.pop(),3.display()1
enter any item3
do u wantto continue y/ny
enter choice of operation1.push(),2.pop(),3.display()1
enter any item4
do u wantto continue y/ny
enter choice of operation1.push(),2.pop(),3.display()3

15150
----
do u want to continue y/n

Result:

Thus output of the Program to implement Stack operations using arrays


is executed.
35
Queue Implementation
EX.NO : 5B
DATE :

AIM: Program to implement Queue operations using arrays

ALGORITHM:
step1:start
step2:(reset rear pointer)
if r=n
then r<-1
else
r<-r+1
step3:(overflow)
if f=r
then write "queue overflow"
return
step4:[insert element]
q[r]<-r
step5:[set the pointer]
if f=0
then f<-1
return

an algorithm for delete element from queue


step1:[underflow]
if f=0
then write queue overflow

step2:[delete element]
y<-q(f)
step3:[queue empty]
if ]<-r<-0
return(y)
step4:[increment front pointer]
if ]=n
then

36
f<-1
else
f<-f+1
return(y)

PROGRAM:
Program to implement Queue operations using arrays
#include<stdio.h>
#define max10
void insert();
void delete();
void display();
int cq[max];
int front=0,rear=0;
void main()
{
int choice;
char ch;
do
{
printf("enter choice for circular queue");
printf("1-insert()
2-delete()
3-display()");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
default:

37
printf("invalid option");
break;
}
printf("do u wantto continue y/n");
fflush(stdin);
scanf("%c",&ch);
}
while(ch=='y'||ch=='y');
}
void insert()
{
int item;
if(rear==max)
rear=1;
else
error++;
if(front==rear)
printf("queue overflow");
else
{
printf("enter any item");
scanf("%d",&item);
cq[rear]=item;
}
if(front==0)
front=1;
}
void delete()
{
int item;
if(front==0)
printf("queue underflow");
else
{
item=cq[front];
printf("the deleted element id %d",item);
}

38
if(front==rear)
{
front=0;
rear=0;
return;
}
if(front==max)
front=1;
else
front=front+1;
}
void dispaly()
{
int i;
if(front==0)
printf("no element inthe queue");
else
{
if(front<rear)
for(i=front;i<=rear;i++)
{
printf("%d",q[i]);
}
else
for(i=front;i>rear;i--)
printf("%d",q[i]);
}
}

39
Output:
1) Insert 2) Delete 3) Display
Enter choice for circular queue 1
Enter any item 14
1) Insert 2) Delete 3) Display
Enter choice for circular queue 1
Enter any item 15
1) Insert 2) Delete 3) Display
Enter choice for circular queue 1
Enter any item 20
1) Insert 2) Delete 3) Display
Enter choice for circular queue 3
14 15 20

Result:

Thus output of the Program to implement Queue operations using arrays


is executed.

40
Infix to Postfix Expression

EX NO : 5C
DATE :

AIM : To implement stack and use it to convert infix to postfix expression.

ALGORITHM :

Step 1: Start the Program.


Step 2: Declare the necessary variables and functions.
Step 3: Set a postfix expression to empty expression and create an empty operator stack.
Step 4: Get an infix expression and read it from left to right.
Step 5: Repeat the following operation till the end of the expression.
i) If the input character/token is an operand place it on the postfix expression.
ii) If the input character is an operator, push it on to the stack. If the stack operator has a
high or equal precedence than the input operator pop that operator from the stack and
place it on to the postfix expression.
Step 6: If the input character is a left parenthesis pop the entire operator from the stack till it
encounters left parenthesis is and push it on the stack.
Step 7: If the input character is a right parenthesis pop all the operators from the stack till
it encounters a right parenthesis and discard both the parenthesis in the postfix expression.
Step 8: Print the postfix expression.
Step 9: Stop the program.

41
PROGRAM :
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
char inf[40], post[40];
int top=0, st[20];
void postfix();
void push(int);
char pop();
void main(void)
{
clrscr();
printf("\n\t Enter the Infix Expression::\n");
scanf("%s",inf);
postfix();
getch();
}
void postfix()
{
int i,j=0;
for(i=0; inf[i]!='\0'; i++)
{
switch(inf[i])
{
case'+':while(st[top]>=1)
post[j++]=pop();
push(1);
break;
case'-':while(st[top]>=1)
post[j++]=pop();
push(2);
break;
case'*': while(st[top]>=3)
post[j++]=pop();
push(3);
break;
case'/': while(st[top]>=3)
post[j++]=pop();
push(4);
break;
case'^': while(st[top]>=4)
post[j++]=pop();
push(5);
break;
case'(':push(0);
break;
case')': while(st[top]!=0)
post[j++]=pop();
top--;
break;
default:post[j++]=inf[i];
42
}
}
while(top>0)
post[j++]=pop();
printf("\n\t Postfix Expression is=>\n\n\t\t%s", post);
}
void push(int ele)
{
top++;
st[top]=ele;
}
char pop()
{
int el;
char e;
el = st[top];
top--;
switch(el)
{
case 1:e='+';
break;
case 2:e='-';
break;
case 3:e='*';
break;
case 4:e='/';
break;
case 5:e='^';
break;
}
return(e);
}

OUTPUT :
Enter the infix expression :
(a+b) * (c-d)

Postfix expression is :
ab+cd-*

RESULT :

Thus the program output has been successfully implemented & verified.

43
Sorting Algorithm
EX.NO : 6A
DATE :
Bubble Sort
AIM: Program to implement Bubble sort

ALGORITHM:
step1: take first two elements of a list and compare them
step2: if the first elements grater than second then interchange else keep the values as it
step3: repeat the step 2 until last comparison takes place
step4: repeat step 1 to 3 until the list is sorted

PROGRAM:
Program to implement Bubble sort
#include<stdio.h>
main()
{
int a[10],i,j,temp,n;
clear();
printf("\n enter the max no.of elements u wanna sort \n");
scanf("%d",&n);
printf("\n enter the elements u want to sort \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
for(i=0;i<n;i++)

44
{
printf("%d\t",a[i]);
}
getch();
}

Output:
enter the max no.of elements u want to sort
5
enter the elements u want to sort
10 20 15 6 40
6 10 15 20 40

RESULT :

Thus the program output has been successfully implemented & verified.
45
EX.NO : 6B
DATE :
Selection Sort
AIM: Program to implement Selection sort

ALGORITHM:

step1: take first a list of unsorted values


step2: consider the first element as minimum element store its index value in a variable
step3:repeat the step 2 until last comparison takes place
step4: compare the minimum with rest of all elements to find minimum value and interchange
the minimum value with the first element
step5: repeat step 3 to 4 until the list is sorted*/

PROGRAM:
Program to implement Selection sort
#include<stdio.h>
main()
{
int a[10],i,j,temp,n;
int min,loc;
clear();
printf("\n enter the max no.of elements u wanna sort \n");
scanf("%d",&n);
printf("\n enter the elements u want to sort \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
min=a[i];
loc=1;
for(j=i+1;j<=n;j++)
{
if(min>a[j])
{
min=a[j];

46
loc=j;
}
}
}
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
for(i=0;i<n;i++)
{printf("%d\t",a[i]);
}
getch();
}

Output:
enter the max no.of elements u wanna sort
5
enter the elements u want to sort
10 20 15 6 40
6 10 15 20 40

RESULT :

Thus the program output has been successfully implemented & verified.
47
EX.NO : 6C
DATE :
Insertion Sort
AIM: Program to implement Insertion sort

ALGORITHM:
step1: take a list of values
step2: compare the first two elements of a list if first element is greater than second
interchange it else keep the list as it is.
step3: now take three elements from the list and sort them as follows
Step4::repeat step 2 to 3 until the list is sorted*/

PROGRAM:
Program to implement Insertion sort
#include<stdio.h>
main()
{
int a[10],i,p,temp,n;
clear();
printf("\n enter the max no.of elements u wanna sort \n");
scanf("%d",&n);
printf("\n enter the elements u want to sort \n");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
a[0]=100;
for(i=2;i<=n;i++)
temp=a[i];
p=i-1;
while(temp<a[p])
{
a[p+1]=a[p];
p=p-1;
}
a[p+1]=temp;
}

48
for(i=1;i<=n;i++)
{
printf("%d\t",a[i]);
}
getch();
}

OUTPUT:
Enter the max no.of elements u want to sort
5
Enter the elements u want to sort
10 20 15 6 40
6 10 15 20 40

RESULT :

Thus the program output has been successfully implemented & verified.
49
EX.NO : 6D
DATE :
Quick Sort
AIM: Program to implement Quick sort

ALGORITHM:
step1: take first a list of unsorted values
step2: take first element as 'pivot'
step3: keep the first element as 'pivot' and correct its position in the list
step4: divide the list into two based on first element
step5: combine the list

PROGRAM:
Program to implement Quick sort
#include<stdio.h>
main()
{
int a[10],i,left,right,n;
int min,loc;
clear();
printf("\n enter the max no.of elements u wanna sort \n");
scanf("%d",&n);
printf("\n enter the elements u want to sort \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
left=0;
right=n-1;
quicksort(a,left,right);
display(a,n);
}
quicksort(int a[],int left,intright)
{
int temp,flag=1,i,j,p;
i=left;
j=right;

50
p=a[left];
if(right>left)
{
while(flag)
{
do
{
i++;
}
while(a[i]<p && i<=right);
while((a[i]>p) && j>left)
j--;
if(j<i)
flag=0;
else
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[lest];
a[left]=a[j];
a[j]=temp;
quicksort[a,left,j-1];
quicksort[a,i,right];
}
}
display(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
getch();
}

51
Output:
enter the max no.of elements u want to sort
5
enter the elements u want to sort
10 20 15 6 40
6 10 15 20 40

RESULT :

Thus the program output has been successfully implemented & verified.

52
EX.NO : 6E
DATE :
Heap Sort
AIM: Program to implement Heap sort

ALGORITHM:
step1: arrange elements of a list in correct form of a binary tree
step2: remove top most elements of the heap
step3: re arrange the remaining elements from a heap this process is continued till we get
sorted list

PROGRAM:
Program to implement Heap sort
#include<stdio.h>
main()
{
int a[10],i,j,n;
int min,loc;
clear();
printf("\n enter the max no.of elements u wanna sort \n");
scanf("%d",&n);
printf("\n enter the elements u want to sort \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
heapsort(a,n);
display(a,n);
}
heapsort(inta[],int n)
{
int temp,i,key,q;
create heap(a,n);
for(q=n;q>2;q--)
{
temp=a[i];
a[i]=a[q];
53
a[q]=temp;
i=1;
key=a[1];
j=2;
if((j+1)<q)
if(a[j+1]>a[j])
j++;
while(j<=(q-1) && a[j]<key))
{
a[i]=a[j];
i=j;
j=2*i;
if((j+1)<q)
if(a[j+1]>a[j])
j++;
else
if(j>n)
j=n;
a[i]=key;
}
}
}

Output:
enter the max no.of elements u want to sort
5
enter the elements u want to sort
10 20 15 6 40
6 10 15 20 40

RESULT :

Thus the program output has been successfully implemented & verified.
54
Searching Algorithm
EX.NO : 7A
DATE :
Binary Search
AIM: Program to implement Binary search

ALGORITHM:
Step 1: Start
Step 2: Read the value of n
Step 3: Enter the number of elements to search
Step 4: for i=1 to n increment in steps of 1Read the value of ith element into array
Step 5: Read the element(x) to be searched
Step 6: search<--binary (a,n,x)
Step 7: if search equal to 0 goto step 7 otherwise goto step 8
Step 8: print successful search
Step 9: stop

PROGRAM:
Program to implement Binary search
#include<stdio.h>
main()
{
int list[10],key,found,num,i;
int low,high,mid;
clrscr();
printf("\n enter the max no.of elements u wanna sort \n");
scanf("%d",&num);
printf("\n enter the elements u want to sort \n");
for(i=0;i<num;i++)
{
scanf("%d",&list[i]);
}
printf("enter the value to be searched");
scanf("%d",&key);
low=o;
high=num-1;
while(low<=high)
{
mid=(low+high)/2;

55
if(key==list[mid])
{
printf("search is successful");
printf("\n the elemnts is %d\n",list[mid]);
found=1;
break;}
if(key<list(mid))
high=mid-1;
else
if(key>list(mid))
low=mid+1;
}
if(found!=1)
printf("seach is unsuccessful");
getch();}

Output:
enter the max no.of elements u want to sort
5
enter the elements u want to sort
12345
enter the value to be searched
3
search is successful
the elements is 3

RESULT :

Thus the program output has been successfully implemented & verified.
56
Searching Algorithm
EX.NO : 7B
DATE :
Linear Search
AIM: Program to implement linear search

ALGORITHM:
Step 1: Start
Step 2: Read the value of n
Step 3: for i=1 to n increment in steps of 1Read the value of ith element into array
Step 4: Read the element(x) to be searched
Step 5: search<--linear(a,n,x)6. if search equal to 0 goto step
Step 7: otherwise goto step 87. print unsuccessful search
Step 8: print successful search
Step 9: stop

PROGRAM:
Program to implement linear search
#include<stdio.h>
main()
{
int list[10],key,found,num,i;
clrscr();
printf(“Enter no. of elements : “);
scanf(“%d”,&num);
printf(“Enter %d elements\n”,num);
for(i=0;i<num;i++)
scanf(“%d”,list[i[);
printf("\n enter the value to be seached \n");
scanf("%d",&key);
for(i=0;i<num;i++)
{
if(key==list[i])
{
printf("\n %d element is found at location%d",list[i],i+1);
found=1;
}

57
}
if(found!=1)
{
printf(search is unsuccessful");
}
getch();
}

Output:
Enter number of elements : 5
Enter 5 elements
15 35 62 45 11
enter the value to be searched
62
62 element is found at location 3

RESULT :

Thus the program output has been successfully implemented & verified.
58

You might also like