Monday 21 October 2013

Program In C For Linear Search In An Array

#include <stdio.h>
#include<conio.h>

int main()
{
int A[10],ele,i,n,pos=-1;
clrscr();
printf("Enter the number of elements in array\n");
scanf("%d",&n);

printf("Enter numbers:\n");

for (i=0 ;i<n;i++)
scanf("%d",&A[i]);

printf("Enter the number to search\n");
scanf("%d",&ele);

for (i=0;i<n;i++)
{
if ( A[i] == ele )
{   pos=i+1;
printf("%d is present at location %d.\n",ele,pos);
break;
}
}
if ( pos==-1 )
printf("%d is not present in array.\n", ele);
getch();
return 0;
}

No comments:

Post a Comment