Escolar Documentos
Profissional Documentos
Cultura Documentos
NAME
: ................................................................................................
SUBJECT
DEPARTMENT
: .................................................................................................
SEMESTER
: ................................................................................................
YEAR
: ................................................................................................
...............................................................................................
APRIL-2014
DEPARTMENT OF INFORMATION TECHNOLOGY
Certified
with
this
is
bonafied
record
of
the
practical
work
done
by _ _ _ _ _
Staff In-Charge
Internal Examiner
External Examiner
Date
Experiment Name
Programs using Conditional Statements
1
2
3
4
5
Program using Functions
6
7
8
9
10
11
12
13
14
Signature
OUTPUT:
Enter a
Number
13
The Given Number is Odd
PROGRA
M:
#include<st
dio.h>
main()
{
int a,b,c;
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);
}
e
l
s
e
printf("The Third Number %d(c) is Biggest\n",c);
}
OUTPUT:
Enter 3
Numbers
5
9
2
The Second Number 9(b) is Biggest
b;
printf("\n %d * %d =
%d\n",a,b,c); break;
case
4:
c=
a/
b;
printf("\n %d / %d =
%d\n",a,b,c); break;
}
}
OUTPUT:
Enter the
Number 1:
15
Enter the
Number 2:
56
1.
Ad
d
2.S
ubt
rac
t
3.
M
ult
ipl
y
4.
Di
vid
e
Enter the
Choice: 2
15 - 56 = -41
printf("Enter the
range\n");
scanf("%d",&n);
i=1;
while
(i<=n
)
{
sum=
sum+
i; i++;
}
printf("\nThe sum of first %d numbers is %d\n",n,sum);
}
}
OUTPUT:
Enter
the
range
16
The sum of first 16 numbers is 136
PROGRAM:
#include
<stdio.h
> main()
{
int n,i,sum=0;
printf("Enter a
Number\n");
scanf("%d",&n);
do
{
i=n
%1
0;
sum
=su
}
m+i
;
n=n
/10;
}while(n>0);
printf("The Sum of digit is %d\n",sum);
OUTPUT:
Enter a
Number
5891
The Sum of digit is 23
dr);
j
=
j
;
}
printf("The Result of sine series is : %f\n",res);
}
OUTPUT:
Enter the
Value of x
0.21
Enter the total
no of terms 5
The Result of sine series is : 0.208460
#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",&c
h);
switch(c
h)
{
case 1:
n=a;
while(a>0)
{
i=a%10;
sum=(s
um*10)
+i;
a=a/10;
lse
}
if(n==sum)
printf("Given Number is Palindrome\n");
e
printf("Given Number is Not Palindrome\n");
n =a;
while(a>0)
case
2
:
n
=
a
;
d
o
{
i=a%10;
sum=sum+
(i*i*i);
a=a/10;
}
while
(a>0)
;
if(n=
=sum
)
printf("Given Number is Armstrong\n");
els
printf("Given Number is Not Armstrong\n");
b
rea
k;
cas
e
3:
m=5;
n=sqrt(a);
for(i=2;i<
=n;i++)
{
if(a%i==0)
{
m
=
0
;
b
r
e
a
k
;
}
}
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
PROGRAM
:
#include<st
dio.h>
main()
{
int i,n,a[10],sum=0;
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]);
for(i=0;i<n;i++)
sum=sum+a[i];
printf("The Sum of Array Elements is %d\n",sum);
}
OUTPUT :
Enter total no. of
Elements 8
Enter Array elements one by
one 15
69
32
10
45
66
32
11
The Sum of Array Elements is 280
#include<st
dio.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]);
}
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
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int arr1[10][10], arr2[10][10];
int arr3[][3]={
{0,0,0},
{0,0,0},
{0,0,0}
};
int i, j;
clrscr();
printf("Enter 3x3 array 1:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter element %d x %d:",i,j);
scanf("%d",&arr1[i][j]);
}
}
printf("Enter 3x3 array 2:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++){
printf("Enter element %d x %d:",i,j);
scanf("%d",&arr2[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
arr3[i][j]=arr1[i][j] + arr2[i][j];
}
}
printf("Addition of Array 1 and Array 2 is: \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("\t%d",arr3[i][j]);
}
printf("\n");
}
getch();
}
OUTPUT:
Enter 3x3 array 1:
Enter element 0 x 0:1
Enter element 0 x 1:1
Enter element 0 x 2:1
Enter element 1 x 0:1
Enter element 1 x 1:1
Enter element 1 x 2:1
Enter element 2 x 0:1
Enter element 2 x 1:1
Enter element 2 x 2:1
Enter 3x3 array 2:
Enter element 0 x 0:1
Enter element 0 x 1:1
Enter element 0 x 2:1
Enter element 1 x 0:1
Enter element 1 x 1:1
Enter element 1 x 2:1
Enter element 2 x 0:1
Enter element 2 x 1:1
Enter element 2 x 2:1
Addition of Array 1 and Array 2 is:
2
#include<stdio.h>
#includ
e<string
.h>
main()
{
char
s[20],s1[20];
printf("Enter a
String\n");
scanf("%s",s);
strcpy(s1,s);
if(strcmp(s,s1)
==0)
printf("The Given String is Palindrome\n");
else
printf("The Given String is Not Palindrome\n");
OUTPUT:
En
ter
a
Str
ing
ma
da
m
The Given String is Palindrome
PROGRAM:
#include<stdio.h>
#include<st
ring.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);
}
Enter a
String1
hai
OUTP Enter a
UT:
String2
hello
The Concatenated String is haihello
OUTPUT:
#include<st
d
i
o.h> void
isleap()
{
int yr;
printf("Enter a
Year\n");
scanf("%d",&yr)
; if(yr%4==0)
printf("Given Year is Leap year");
else
printf("Given Year is Not a Leap year");
}
main()
{
isleap();
}
OUTPUT:
OUTPUT:
Enter a year
1965
Given year is
Not a leap year
#include<stdio.h>
#include<m
ath.h> float
area()
{
in
t
a,
b,
c;
fl
o
at
s,
ar
;
printf("Enter 3
Sides\n"); scanf("%d
%d%d",&a,&b,&c);
s=(a+b+c)/2;
ar=sqrt(s*(s-a)*(sb)*(s-c)); return ar;
}
main()
{
fl
o
at
a;
a
=
a
r
e
a
()
;
printf("The Area of Triangle is %f\n",a);
}
OUTPUT:
E
nt
er
3
Si
de
s
12
8
7
The Area of Triangle is 19.748418
PROGRAM:
#include<stdio.h>
void sorting(int a[],int n)
{
int i,j,t;
for(i=0;i<n1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[
i];
a[i]
=a[j
];
a[j]
=t;
}
}
}
printf("Array Elemets before
sorting\n"); for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
main()
{
:
int i,a[10],n;
p
r
i
n
t
f
(
"
E
Elemets:\n");
for(i=0;i<n;i++)
printf("%d\t",
a[i]); printf("\n");
s=small(a,n);
printf("The Smallest element of given array is %d",s);
}
OUTPUT:
E
n
t
e
r
t
o
t
a
l
n
o
.
o
f
e
l
e
m
e
n
t
s
5
E
n
t
e
r
A
r
r
a
y
PROGRAM
:
#include<stdi
o.h> int
actorial(int n)
{
if(n==0 ||
n==1)
return
1;
else
return n*factorial(n-1);
}
main()
{
int n;
printf("\nEnter a
Number\n");
scanf("%d",&n);
printf("\nThe factorial of %d is %d\n",n,factorial(n));
}
OUTPUT:
Enter a Number
6
The factorial of 6 is 720
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int c,i,id;
char name[20];
FILE *fp;
int n;
int search(FILE *fp,int id);
void display(FILE *fp);
4.Delete
5. Exit
4.Delete
5. Exit
4.Delete
5. Exit
1
1 anu
2 akash
3 adhavan
4 jai
What would you like to do now?:
1.Display
2.Search
3.Modify
2
Enter id to search:2
2 akash
What would you like to do now?:
1.Display
2.Search
3.Modify
3
4.Delete
PROGRAM
#include <stdio.h>
struct hardwareData
{
int recordNum;
char toolname[20];
int quantity;
double cost;
};
//functions
int enterChoice( void );
void printList( FILE *readPtr );
void updateRecord( FILE *fPtr );
void newRecord( FILE *fPtr );
void deleteRecord( FILE *fPtr );
int main()
5. Exit
{
FILE *cfPtr;
int choice;
if ((cfPtr = fopen( "hardware.dat", "ab+" )) == NULL )
{
printf( "File could not be opened.\n" );
}
else
{
while ( choice != 4 )
{
switch ( choice = enterChoice() )
{
//adds a new record, or upates one
case 1:
newRecord ( cfPtr );
break;
//deletes a record
case 2:
deleteRecord ( cfPtr );
break;
//prints list
case 3:
printList ( cfPtr );
break;
//display message if user does not enter valid choice
default:
printf( "Incorrect choice\n" );
break;
}
}
fclose( cfPtr );
}
//system("pause");
return 0;
}
void printList( FILE *fPtr )//print function
{
struct hardwareData hardware;
fseek(fPtr, 0, SEEK_SET);
printf("%-10s%-16s%-10s%10s\n", "Number", "Name", "Quantity", "cost");
while(fread(&hardware, sizeof( struct hardwareData), 1, fPtr)) {
if(hardware.recordNum != 0){
printf("%-10d%-16s%-10d%-10.2lf\n", hardware.recordNum,
hardware.toolname,
hardware.quantity, hardware.cost);
}
}
}
OUTPUT:
Enter your choice
1- Add a new tool,or update an existing tool
2- Delete a tool
3- Print the list
4- End program
?1
Enter record number to create(1-100):55
Enter tool name,quantity,cost
RAM,2gb,3000
Enter your choice
1-Add a new tool,or update an existing tool
2-Delete a tool
3-Print the list
4-End program
?1
Enter record number to create(1-100):68
Enter tool name,quantity,cost
ROM,1gb,2000
Enter your choice
1-Add a new tool,or update an existing tool
2-Delete a tool
3-Print the list
4-End program
?1
Enter record number to create(1-100):23
Enter tool name,quantity,cost
Joystick,2mb,5000
Enter your choice
Cost
0.00
0.00
3.00
0.00
#include<
stdio.h>
struct
student
{
int
rno,m1,m
2,m3;
float avg;
char name[20],dept[10];
};
main()
{
struct student s;
printf("Enter the Student
Details:\n"); printf("Enter
the Stuent roll no:\n");
scanf("%d",&s.rno);
printf("Enter the Stuent
Name:\n");
scanf("%s",&s.name);
printf("Enter the Stuent
Dept:\n");
scanf("%s",&s.dept);
printf("Enter the 3 marks:\n");
scanf("%d%d
%d",&s.m1,&s.m2,&s.m3);
s.avg=(s.m1+s.m2+s.m3)/3;
printf("The Student Average is :%f\n",s.avg);
}
PROGRAM:
#include<stdio.h>
#include<conio.h>
int stack[20],top=0;
char inf[40],post[40];
void push(int);
void postfix();
char pop();
void main(void)
{
clrscr();
printf("\t\t\t****INFIX TO POSTFIX****\n\n");
printf("Enter the infix expression :: ");
scanf("%s",inf);
postfix();
getch();
}
void postfix()
{
int i,j=0;
for(i=0;inf[i]!=NULL;i++)
{
switch(inf[i])
{
case '+':
while(stack[top]>=1)
post[j++]=pop();
push(1);
break;
case '-':
while(stack[top]>=1)
post[j++]=pop();
push(2);
break;
case '*':
while(stack[top]>=3)
post[j++]=pop();
push(3);
break;
case '/':
while(stack[top]>=3)
post[j++]=pop();
push(4);
break;
case '^':
while(stack[top]>=4)
post[j++]=pop();
push(5);
break;
case '(':
push(0);
break;
case ')':
while(stack[top]!=0)
post[j++]=pop();
top--;
break;
default:
post[j++]=inf[i];
}
}
while(top>0)
post[j++]=pop();
printf("\nPostfix Expression is :: %s",post);
}
void push(int ele)
{
top++;
stack[top]=ele;
}
char pop()
{
char e;
e=stack[top];
top--;
switch(e)
{
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*/
Manual Calculation
SE EXPRESSION
(
A
+
B
)
/
(
C
D
+
E
)
+
F
G
STACK
RESULT FIELD
(
+,(
+,(
),(
/
(,/
(,/
-,(,/
-,(,/
+,(,/
+,(,/
),+,(,/
+,/
+
-
A
A
AB
AB+
AB+
AB+C
AB+C
AB+CD
AB+CDAB+CD-E
AB+CD-E+
AB+CD-E+/
AB+CD-E/F
AB+CD-E/F+
AB+CD-E/F+G
AB+CD-E/F+G-
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 50
int stack[MAX];
char post[MAX];
int top = -1;
void pushstack(int tmp);
void calculator(char c);
void main()
{
int i;
clrscr();
printf("Insert a postfix notation :: ");
gets(post);
for (i = 0; i < strlen(post); i++)
{
if (post[i] >= '0' && post[i] <= '9')
{
pushstack(i);
}
if (post[i] == '+' || post[i] == '-' || post[i] == '*' || post[i] == '/' || post[i] == '^')
{
calculator(post[i]);
}
}
printf("\n\nResult :: %d", stack[top]);
getch();
}
void pushstack(int tmp)
{
top++;
stack[top] = (int)(post[tmp] - 48);
}
void calculator(char c)
{
int a, b, ans;
a = stack[top];
stack[top] = '\0';
top--;
b = stack[top];
stack[top] = '\0';
top--;
switch (c)
{
case '+':
ans = b + a;
break;
case '-':
ans = b - a;
break;
case '*':
ans = b*a;
break;
case '/':
ans = b / a;
break;
case '^':
ans = b^a;
break;
default:
ans = 0;
}
top++;
stack[top] = ans;
}
OUTPUT:
Insert a postfix notation: 1453*+8Result:11
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
push();
void pop();
void display();
struct node
{
int data;
struct node *next;
}*top=NULL;
void main()
{
int ch;
clrscr();
printf("\n\n1.Push\n\n2.Pop\n\n3.Display");
do
{
printf("\n\nEnter your Choice :: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
printf("\n\nContents of stack :: \t");
display();
break;
default:
printf("\n\nInvalid Choice......");
getch();
exit(0);
}
}while(ch<4);
getch();
}
push()
{
int x;
struct node *newnode;
newnode=malloc(sizeof(struct node));
OUTPUT:
1.Push
2.Pop
3.Display
Enter your Choice :: 1
Enter the number to be pushed into the stack :: 5
Number pushed is 5
Enter your Choice :: 1
Enter the number to be pushed into the stack :: 10
Number pushed is 10
Enter your Choice :: 3
Contents of stack :: 10 , 5 ,
Enter your Choice :: 2
Deleted element is 10
Enter your Choice :: 3
Contents of stack :: 5 ,
Enter your Choice :: 5
Invalid Choice......
case 2:
elem = Delete();
if (elem != -1)
printf(" Deleted Node(From Front)with the Data: %d\n", elem);
break;
case 3:
printf("Linked List Implementation of Queue: Status:\n");
Display();
break;
case 4:
printf("\n\n Terminating \n\n");
break;
default:
printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
printf("\n\n\n\n Press a Key to Continue . . . ");
getch();
} while (opn != 4);
}
void Insert(int info) {
NODE *temp;
temp = (NODE *) malloc(sizeof(NODE));
if (temp == NULL)
printf(" Out of Memory !! Overflow !!!");
else {
temp->data = info;
temp->link = NULL;
if (front == NULL) {
front = rear = temp;
} /* First Node? */
else {
rear->link = temp;
rear = temp;
} /* Insert End */
printf(" Node has been inserted at End Successfully !!");
}
}
int Delete() {
int info;
NODE *t;
if (front == NULL) {
printf(" Underflow!!!");
return -1;
} else {
t = front;
info = front->data;
if (front == rear)
rear = NULL;
front = front->link;
t->link = NULL;
free(t);
return (info);
}
}
void Display() {
NODE *t;
if (front == NULL)
printf("Empty Queue\n");
else {
t = front;
printf("Front->");
while (t) {
printf("[%d]->", t->data);
t = t->link;
}
printf("Rear\n");
}
}
OUTPUT:
### Linked List Implementation of Queue operation ###
Press 1-Insert, 2-Delete, 3-Display, 4-Exit
Your option ? 1
Read the element to be inserted ? 10
Node has been inserted at end successfully !!
Press a Key to continue .
Your option ? 1
Read the element to be inserted ? 20
Node has been inserted at end successfully !!
Press a Key to continue .
Your option ? 1
Read the element to be inserted ? 30
Node has been inserted at end successfully !!
Press a Key to continue .
Your option ? 1
Read the element to be inserted ? 40
Node has been inserted at end successfully !!
Press a Key to continue .
Your option ? 3
Linked List Implementation of Queue:Status:
Front->[10]->[20]->[30]->[40]->Rear
Press a Key to continue .
Your option ? 4
Terminating
Press a Key to continue .
SORTING ALGORITHMS
BUBBLE SORT
PROGRAM :
#include<stdio.h>
#include<conio.h> void
bubble(int [],int); void
main()
{
int a[20],i,n;
clrscr();
printf("Enter the number of items in the array");
scanf("%d",&n);
printf("Enter the data in the array");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
} bubble(a,n);
getch();
}
void bubble(int a[],int n)
{
int i,temp,j,p;
for(i=1;i<n;i++)
{
for(p=0;p<n-i;p++)
{
if(a[p]>a[p+1])
{
temp=a[p];
a[p]=a[p+1];
a[p+1]=temp;
}
}
}
for(i=0;i<n;i++)
printf("\n%d",a[i]);
}
OUTPUT:
Enter the number of items in the array 5
35
25
15
65
5
Enter the data in the array
5
15
25
35
65
SELECTION SORT
PROGRAM :
#include<stdio.h>
#include<conio.h> void
select(int [],int); void
bubble(int [],int); int
min(int [],int,int);
void main()
{
int a[20],i,n;
clrscr();
printf("Enter the number of items in the array");
scanf("%d",&n);
printf("Enter the data in the array");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
bubble(a,n);
select(a,n);
getch();
}
void bubble(int a[],int n)
{
int i,temp,p;
for(i=1;i<n;i++)
{
for(p=0;p<n-i;p++)
{
if(a[p]>a[p+1])
{
temp=a[p];
a[p]=a[p+1];
a[p+1]=temp;
}
}
}
printf("\nData After Bubble Sort");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
}
void select(int a[],int n)
{
int i,loc,temp;
loc=0; temp=0;
for(i=0;i<n;i++)
{
loc=min(a,i,n);
temp=a[loc];
a[loc]=a[i];
a[i]=temp;
}
printf("\nData After Selection Sort");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
}
int min(int a[],int lb,int ub)
{
int m=lb;
while(lb<ub)
{
if(a[lb]<a[m])
{
m=lb;
}
lb++;
}
return m;
}
OUTPUT:
Enter the number of items in the array 4
Enter the data in the array
10
23
89
45
Data After Bubble sort
10
23
45
89
Data After Selection sort
10
23
45
89
INSERTION SORT
PROGRAM :
#include<stdio.h>
int main(){
int i,j,s,temp,a[20];
printf("Enter total elements: ");
scanf("%d",&s);
printf("Enter %d elements: ",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=1;i<s;i++){
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0)){
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("After sorting: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
return 0;
}
OUTPUT:
Enter total elements: 5
Enter 5 elements: 3 7 9 0 2
After sorting: 0 2 3 7 9
QUICK SORT
PROGRAM :
#include<stdio.h>
void quicksort(int [10],int,int);
int main(){
int x[20],size,i;
printf("Enter size of the array: ");
scanf("%d",&size);
printf("Enter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("Sorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
return 0;
}
void quicksort(int x[10],int first,int last){
int pivot,j,temp,i;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
OUTPUT:
Enter size of the array: 5
Enter 5 elements: 3 8 0 1 2
Sorted elements: 0 1 2 3 8
LINEAR SEARCH
PROGRAM :
#include<stdio.h>
#include<conio.h>
void main()
{
int
a[10],i,no,item,flag=0;
char c=y,ch;
clrscr();
while(c==y||Y)
{
printf(\nEnter the sizeof sorting);
scanf(%d,&no);
printf("Enter the data in the array");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched");
scanf("%d",&item);
for(i=0;i<10;i++)
{
if(item==a[i])
{
flag=1;
break;
}
}
if(flag==0)
printf("Element Not Found");
else
printf("Element Found at Position =%d",i);
getch();
}
OUTPUT:
Enter the size of sorting 5
Enter the elements of the array 3 2 1 6 5
Enter the element to be searched 4
Element 4 is Not Found
Enter the element to be searched 6
Element 6 is in the position 4
BINARY SEARCH
PROGRAM :
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,mid,beg,i,end,item,loc=-1;
clrscr();
printf("Enter the number of elements to be entered\n");
scanf("%d",&n);
printf("Enter the elements in ascending
order");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched");
scanf("%d",&item);
beg=0; end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
if(item==a[mid])
{
loc=mid;
break;
}
else
if(a[mid]<item
) beg=mid+1;
else
end=mid-1;
}
if(loc==-1)
printf("Element Not Present"); else
printf("Element found at =%d",loc);
getch();
}
OUTPUT: