Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

C program for linear search

C program for linear search. The following code implements linear search (Searching algorithm) which is used to find whether a given number is present in an array and if it is present then at what location it occurs. It is also known as sequential search.

C program for linear search

#include <stdio.h>
#include <stdlib.h>

int main()
{   int n,i,arr[100],item,temp,count=0;
    printf("\n This is a simple program for linear search \n");
    printf("\n Enter the size of array you want\n");
    scanf("%d",&n);
    printf("\n Now Enter the elements of array\n");
    for(int i=0;i<n;i++)
        scanf("%d",&arr[i]);
    printf("Array elements entered by you is: ");
    for(i=0;i<n;i++)
       printf("%d\t",arr[i]);
       printf("\n\n Now enter the element to be searched \n");
       scanf("%d",&item);
       for ( i=0;i<n;i++)
       {
           if(item==arr[i])
              {
               printf("\n Item found at position %d",i+1);
               count++;
              }
          temp=i;
       }
              if(temp==n-1 && count==0)
    printf("\n Item not found in the given array list");
 return 0;
}

Output:

C program for linear search
Output:1
C program for linear search
Output:2
C program for linear search2
Output:3

Now, take a look for another search algorithm and program for binary search.

C program for binary search

Leave a Comment

error: