Waliullah8328

Linear_Search

Jun 23rd, 2021 (edited)
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.60 KB | None | 0 0
  1. #include<stdio.h>
  2.  int Linear_Search(int a[],int n,int x)
  3.  {
  4.      int i;
  5.      for(i=1;i<=n;i++)
  6.      {
  7.          if(a[i] == x)
  8.          {
  9.              return i;
  10.          }
  11.      }
  12.      return -1;
  13.  }
  14. int main()
  15. {
  16.   int a[30],n,x,i;
  17.   printf("Enter how many input you insert : ");
  18.   scanf("%d",&n);
  19.   printf("Input your %d Numbers : \n",n);
  20.   for(i=1;i<=n;i++)
  21.   {
  22.      scanf("%d",&a[i]);
  23.   }
  24.   printf("Which number you want to search : ");
  25.   scanf("%d",&x);
  26.  if(Linear_Search(a,n,x) == -1)
  27.  {
  28.  
  29.      printf("Not Found\n");
  30.  }
  31.  else printf("Found in index of %d",Linear_Search(a,n,x));
  32. }
Add Comment
Please, Sign In to add comment