adplus-dvertising

C program to implement Linear Search

frame-decoration

C program to implement Linear Search

Problem Description

This C Program implements linear search. Linear search is also called as sequential search. Linear search is a method for finding a particular value in a list, that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found.

C program to implement Linear Search - Source code
     
                
  /*
 C program to input N numbers and store them in an array.
 Do a linear search for a given key and report success
 or failure.
 */

#include <stdio.h>

void main()
{
    int arr[10];
    int i, n, key, found = 0;

    printf("Enter the size of array to search \n");
    scanf("%d", &n);
    printf("Enter the elements of array one by one \n");
    for (i = 0; i < n; i++)
    {
        scanf("%d", &arr[i]);
    }
    printf("Input array is \n");
    for (i = 0; i < n; i++)
    {
        printf("%d\n", arr[i]);
    }
    printf("Enter the element to be searched in array\n");
    scanf("%d", &key);

    /*  Linear search begins */
    for (i = 0; i < n ; i++)
    {
        if (key == arr[i] )
        {
            found = 1;
            break;
        }
    }
    if (found == 1)
        printf("Element is present in the array\n");
    else
        printf("Element is not present in the array\n");
}
     
      

Program Output

10
Enter the elements of array one by one
56
25
48
69
12
2
8
10
99
45
Input array is
56
25
48
69
12
2
8
10
99
45
Enter the element to be searched in array
10
Element is present in the array