Implementation of Linear search.
Source Code:
#include<stdio.h>
int main(){
int ary[20], n, i, searchElement;
printf("Enter size of array:");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&ary[i]);
printf("Enter search element:");
scanf("%d",&searchElement);
for(i=0;i<n;i++){
if(ary[i]==searchElement){
break;
}
}
if(i==n)
printf("Element not found.");
else
printf("Element found at %d position.",i);
return 0;
}
int main(){
int ary[20], n, i, searchElement;
printf("Enter size of array:");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&ary[i]);
printf("Enter search element:");
scanf("%d",&searchElement);
for(i=0;i<n;i++){
if(ary[i]==searchElement){
break;
}
}
if(i==n)
printf("Element not found.");
else
printf("Element found at %d position.",i);
return 0;
}
Enter size of array:5
Enter 5 elements:
9 6 8 2 5
Enter search element:6
Element found at 1 position.
Enter 5 elements:
9 6 8 2 5
Enter search element:6
Element found at 1 position.