UncleCoder.com

UncleCoder.com

Free programming examples and instructions

C Program to implement Linear Search

C Program for how to implement Linear Search

by Krishna


Posted on 27 Jul 2018 Category: C Views: 1911


 C Program to implement Linear Search

Here is source code of the C Program to implement  Linear Search Algorithm. The program is successfully compiled and tested . The program output is also shown below.

#include <stdio.h>
 
void main()
{  int num;
 
    int i,  keynum, found = 0;
 
    printf("Enter the array limit : ");
    scanf("%d", &num);
    int array[num];
    printf("Enter the array elements : \n");
    for (i = 0; i < num; i++)
    {
        scanf("%d", &array[i]);
    }
 
    printf("Enter the element to be searched ");
    scanf("%d", &keynum);
    /*  Linear search begins */
    for (i = 0; i < num ; i++)
    {
        if (keynum == array[i] )
        {
            found = 1;
            break;
        }
    }
    if (found == 1)
        printf("Element is present ");
    else
        printf("Element is not present in the array\n");
}

 

OUTPUT



Leave a Comment:


Click here to register

Popular articles