Search for key Element in an Array using Linear Search
Download the Program#include<stdio.h>
void main()
{
int i, j, n, a[100], key;
printf("\nEnter value of n:");
scanf("%d", &n);
printf("\nEnter array elements:");
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
printf("\nEnter key:");
scanf("%d", &key);
for(i=0; i<n; i++)
{
if(a[i] == key)
break;
}
if(i<n)
printf("\nKey found at position %d", i+1);
else
printf("\nNot found");
}
Output:
Enter value of n: 6
Enter array elements: 1 3 2 4 5 1
Enter key: 3
Key found at position 2
No comments:
Post a Comment