Search in Array

Search a given number in an array using Linear Search Algorithm.
Linear search is also called as sequential search. Linear search is a method for finding a particular value in an array, that consists of checking every element, one at a time and in sequence, until the desired one is found.

/*
Array Linear Search
Shaswat Saxena
10/11/2017
*/
#include <stdio.h>
#include <conio.h>
void main ()
{
  int i,j,size,temp,term,pos;
  clrscr();
  pos=0;
  printf("Enter the total numbers you will enter :\n");
  scanf("%d",&size);
  int num[size];
  for (i=0; i < size; i++) {
    printf("Enter a number\n");
    scanf("%d",&num[i]);
  }
  printf("Enter the  number to search\n");
  scanf("%d",&term);
  //Search in Array
  for (i=0 ;i < size; i++)
    {
        if (num[i] == term) {
          pos=i+1;
          break;
        }
      }
  if (pos==0) {
    printf("Number not found in the list .\n");
  }
  else {
    printf("%d found successfully at position %d .\n",term,pos);
  }
  getch();
}

Output

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

Recommended for You  Calculate Factorial of a Given Number

Leave a Reply

Close Menu