Thursday, 1 March 2012

Linear Search

Q. Write a C program to search an element in a given list of elements using Liner Search.


Ans.


/*c program for linear search*/
#include<stdio.h>
#include<conio.h>
int main()
{
  int arr[20];
  int i,size,sech;
  printf("\n\t-- Linear Search --\n\n");
  printf("Enter total no. of elements : ");
  scanf("%d",&size);
  for(i=0; i<size; i++)
  {
    printf("Enter %d element : ",i+1);
    scanf("%d",&arr[i]);
  }
  printf("Enter the element to be searched: ");
  scanf("%d",&sech);
  for(i=0; i<size; i++)
  {
    if(sech==arr[i])
    {
      printf("Element exits in the list at position : %d",i+1);
      break;
    }
  }
  getch();
  return 0;
}


/**************** OUTPUT ****************/


Searching element through Linear Search

No comments:

Post a Comment