Wednesday, October 20, 2021

Program 3- Linear Search in C Language

Program 3: Linear search
***********************************************

#include <stdio.h>

int main()

{

    int a[20],i,x,n, c;

    printf("How many elements you are going to enter?");

    scanf("%d",&n);

    printf("Enter array elements:");

    for(i=0;i<n;++i)

        scanf("%d",&a[i]);

         printf("\n Enter element to search:");

    scanf("%d",&x);

       for(i=0;i<n;++i)

        if(a[i]==x)

        { printf("Element %d found at index %d \n",x, i);

            c=c+1;

                    }

         if(c>0)

        printf("Element %d found %d times ",x, c);

    else

        printf("Element not found");

    return 0;

}

Outputs