Dark mode logo
Last Updated:
Implement linear search in c

C program to implement linear search in an array

Algorithm

Input: Input size of the array, elements of the array and element to be searched
output: Searches for a user input element in the array and displays its index if any

  1. Start
  2. Input the size of the array say n and enter the elements of the array a[n]
  3. Input the element to be searched
  4. i = 0
  5. found = 0
  6. while ( i < n )
    5.1 if a [ i ] = = search
      5.1.1 print "Found the required element at index " i
      5.1.2 found = 1
    5.2 i++
  7. if ( found = = 0)
    7.1 print "The element not found in the array"
  8. stop

C Program

#include <stdio.h>
int main()
{
int n, found = 0, search;
printf("Enter the size of array: ");
scanf("%d", &n);
int a[n];
printf("Enter the elements one by one\n");
for (int i = 0; i<n; i++)
    scanf("%d", &a[i]);
printf("Enter the element to be searched:");
scanf("%d", &search);
for (int i = 0; i < n; i++) {
      if (a[i] == search) {
         printf("Found the required element at index %d\n", i);
         found = 1;
         } }
      if (found == 0) {
         printf("The element %d not found in the array", search);
         }

}


Note: This Program was created using visual studio code. The Program file can be downloaded by clicking here

Flowchart

Flow-chat of a program to implement linear search in an array

Note: This Flowchart was created using a program called Raptor. The flowchart file can be downloaded by clicking here

Comments