C program to search an element in an array.
In this program, I have discussed about the C program to search an element in an array.
This method is also known as linear search.
#include<stdio.h>int main()
{ int ar[20],i,j,key,n,pos,fl=0;;
printf("Enter the size of the array \n");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter an element :\n");
scanf("%d",&ar[i]);
}
printf("Enter the key value to search :\n");
scanf("%d",&key);
for(j=0;j<n;j++)
{ if(ar[j]==key)
{
printf("Search Successfull\n");
printf("Element %d is found in %d position of the array\n",key,j);
fl=1;
break;
}
}
if(fl!=1)
{ printf("Not found !!!");
}
}
Just copy and paste the program in your desired compiler (Dev C++ is more preferable) and run it.
Enjoy programming in C.
0 comments:
Post a Comment