C Program to linear search for a number

What is a linear search algorithm?

Linear search in C is used to find whether a number is present in an array. It is also known as a sequential search. There are so many search algorithms in the C language. The most simple Algorithm is for Linear Search. In linear search, the algorithm compares the entered element with array elements till the match is found or the array gets completed.

For example, let us take some numbers "1,2,3,4,5" in an array 'a'. if we want to search number '5' using linear search, the search start searching the number '5' comparing the number with a[0], which is '1' then it moves through 2, 3, 4, and lastly it gets a result at 'a[4]' match found, and algorithm gets completed. 

  • The best case in linear search when the item found first of the array
  • The worst is the item found last of the Array.

How linear search implemented in C?

In this c program, after including the header libraries, and insert all the values into the array. After inserting all elements, we need the user to enter the number that needs to search using linear search.

After assigning that number into a variable, compare each element of the array with the variable user inputs using 'for loop' and display output with a suitable message.

ALGORITHM

STEP 1: Includes the Header Libraries into the C program to use the built-in functions.

STEP 2: Start the main() function to start the execution of the C program.

STEP 3: Initialize the Array and the variables to be used in the program.

STEP 4: Accept the number of elements needed from the user using printf and scanf functions and store that integer value to a variable.

STEP 5: Accept and add the elements into the Array using a 'for loop'.

STEP 6: Display the elements in the Array using a for loop and printf.

STEP 7: Accept the element to be searched from the user and save that to a variable.

STEP 8: Open a for loop from zero to the number of terms and increment it by one for Linear Search with each element.

STEP 9: Compare the search element with the Array elements using the if condition and make the found one, when the number in the Array

STEP 10: After the loop check, if the 'found = 1', the element found else print not found.

C Source Code

                                          #include <stdio.h>


void main() {
  int array[10];
  int i, N, keynum, found = 0; /* declares array and variables keynum found etc as integer */
 
  printf("Enter the value of N\n"); /* enters the values by user */
  scanf("%d", & N);
  printf("Enter the elements one by one\n"); /* enter the values into a array and print the numbers */
  for (i = 0; i < N; i++) /*using linear search we compare the number with other elements in array */ {
    scanf("%d", & array[i]);
  }
  printf("Enter the element to be searched\n"); /* enters the values by user */
  scanf("%d", & keynum);
  for (i = 0; i < N; i++) /*using linear search we compare the number with other elements in array */ {
    if (keynum == array[i]) {
      found = 1;
      break;
    }
  }
  if (found == 1)
    printf("SUCCESSFUL SEARCH\n"); /* prints the result success of failure */
  else
    printf("Search is FAILED\n");
} /* End of main */
                                      

OUTPUT

RUN 1

Enter the value of N
5

Enter the elements one by one
23
12
56
43
89

Enter the element to be searched
56
SUCCESSFUL SEARCH

RUN 2

Enter the value of N
3

Enter the elements one by one
456
213
879

Enter the element to be searched
1000

Search is FAILED