All articles
c

A program to explain LINEAR SEARCH using FUNCTIONS

Share this article

Share on LinkedIn Share on X (formerly Twitter)

C Programming Language 079

  • Write a program to explain LINEAR SEARCH using FUNCTIONS

Program

#include <stdio.h>
 
void linear(int b[], int n);
void main()
{
    int a[30], i, n;
    printf("Enter how many numbers you want to input: ");
    scanf("%d", &n);
 
    printf("Enter elements of array:\n");
    for (i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
 
    linear(a, n);
 
    printf("\n");
}
 
void linear(int b[], int m)
{
    int j, key;
    printf("Enter element to be searched: ");
    scanf("%d", &key);
    for (j = 0; j < m; j++)
    {
        if (key == b[j])
        {
            printf("Element found at position %d", j + 1);
            break;
        }
    }
 
    if (j == m)
    {
        printf("Element not found");
    }
}

Result

Enter how many numbers you want to input: 5
Enter elements of array:
5 4 3 2 1
Enter element to be searched: 2
Element found at position 4

Comments