3.02.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

5 comments:

  1. some samples for linear search that will find the perfect square in the given array....???

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Great answer for my tutorial

    ReplyDelete