Wednesday, 18 March 2015

Program to search an element using linear search Using C


# include <stdio.h>
# include <conio.h>
void main()
{
int a[10], i, n, item, flag = 0 ;
clrscr() ;
printf("Enter the limit : ") ;
scanf("%d", &n) ;
printf("\nEnter the numbers :\n\n") ;
for(i = 0 ; i < n ; i++)
scanf("%d", &a[i]) ;
printf("\nEnter the element to be searched : ") ;
scanf("%d", &item) ;
for(i = 0 ; i < n ; i++)
{
if(item == a[i])
{
flag = 1 ;
break ;
}
}
if(flag == 1)
printf("\nThe element is found at location : %d", i + 1) ;
else
printf("\nThe element is not found") ;
getch() ;
}


RUN 1 :
~~~~~~~
Enter the limit : 5
Enter the numbers :
20 40 30 50 10
Enter the element to be searched : 30
The element is found at location : 3

RUN 2 :
~~~~~~~
Enter the limit : 5
Enter the numbers :
20 40 30 50 10
Enter the element to be searched : 70
The element is not found

No comments:

Post a Comment