Linear Search in C


Q. Write a C program for Linear Search.

#include <stdio.h>
#include <conio.h>
 
void main()
{
    int a[25], search, size, i;
    printf("Enter number of elements in array: ");
    scanf("%d", &size);
    printf("Enter the integers: ");
    for (i = 0; i < size; i++)
    {
        scanf("%d", &a[i]);
    }
    printf("Enter the number to search: ");
    scanf("%d", &search);
    for (i = 0; i < size; i++)
    {
        if (a[i] == search)  
        {
            printf("%d is present in the array.", search);
            break;
        }
    }
    if (i == size)
    {
        printf("%d isn't present in the array.", search);
    }
    getch();
}

Q. Write a C program for Linear Search.

1. Start
2. Read the number of elements `size` of the array
3. Initialize an integer array `a` with size `size`
4. For `i` from 0 to `size - 1` do:
    - Read the integer `a[i]`
5. Read the number to search `search`
6. For `i` from 0 to `size - 1` do:
    - If `a[i] == search` then:
    - Print: "`search` is present in the array."
    - Break from the loop
7. If loop completes without finding `search` (i.e., `i == size`),
    - Print: "`search` isn't present in the array."
8. End 



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.