C Program to Search an Element in an Array (Linear Search)
#include<stdio.h>
#include<stdlib.h>
void main()
{
int a[50],n,i,ele;
do
{
printf("\nEnter no. of elements in between 1 and 50: ");
scanf("%d",&n);
}while(n<1 || n>50);
printf("\nEnter the elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter an element for search: ");
scanf("%d",&ele);
for(i=0;i<n;i++)
{
if(a[i]==ele)
{
printf("\n%d is found.",ele);
exit(0);
}
}
printf("\n%d is NOT found.",ele);
}
Comments
Post a Comment