Q:

C program to implement Linear Search

belongs to collection: C programs - Searching and Sorting

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C program to implement Linear Search - Source code

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
<< C program to read an Array and Search for an eleme...