Write c program for searching an element from the given array using Linear search

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

//To define function
void search(int a[], int n);

void main()
{
   int a[20],i,n;
   printf("Enter how many number of elements in list :- ");
   scanf("%d",&n);
   printf("\nEnter the elements");
   for(i=0;i<n;i++)
   {
    printf("\nEnter element %d: ",i+1);
        scanf("%d",&a[i]);
   }
   search(a,n);
   getch();
}

//To search element
void search(int a[], int n)
{
int s,i,flag=0;
   printf("\nEnter the number which you want to search:");
   scanf("%d",&s);
   for(i=0;i<n;i++)
   {
    if(a[i]==s)
      {
      flag=1;
         break;
      }
   }
   if(flag==1)
   printf("\n%d is found at %d place from given list.",s,i+1);
   else
   printf("\n%d does not exist in the given list.",s);
}

0 Comments