Data Structure Linear Search

Program on Linear Search is given below:

//Linear Search

#include<stdio.h>
void linearsearch(int a[],int n,int data)
{
    int flag =0;
    for(int i=0;i<n;i++)
    {
        if(a[i]==data)
        {
            flag =1;
            printf(" data is found and the data is at index no %d",i);
            break;
        } 
    }
    if(flag == 0)
    {
        printf("not found");
    }
}
int main()
{
    int a[100],n,data;
    printf("enter the size of array");
    scanf(" %d",&n);
    printf("enter the array");
     for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("enter the data to search");
    scanf("%d",&data);
    linearsearch(a,n,data);
    
}

Output

enter the size of array5
enter the array13 43 55 3 6
enter the data to search3
 data is found and the data is at index no 3

Leave a Reply

Your email address will not be published. Required fields are marked *

Solverwp- WordPress Theme and Plugin