TO FIND AN ELEMENT USING LINEAR SEARCH

//TO FIND AN ELEMENT USING LINEAR SEARCH//
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,item,loc,A[10];
clrscr();
printf(“Enter the total number of elements of the array: “);
scanf(“%d”,&n);
printf(“Enter the elements in the array:\n”);
for(i=1;i<=n;i++)
{
printf(“Element number [%d] is “,i);
scanf(“%d”,&A[i]);
}
printf(“Enter the element which you want to search in the array: “);
scanf(“%d”,&item);
//SEARCHING BEGINS//
A[n+1]=item;
loc=1;
while(A[loc]!=item)
{
loc=loc+1;
}              //SEARCHING ENDS//
if(loc==n+1)
{
loc=0;
printf(“Element is not present in the given array”);
}
else
{
printf(“Element is present at [%d] location”,loc);
}
getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *