Advertisement
tampurus

1.1 Linear search

Dec 11th, 2021 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.85 KB | None | 0 0
  1. #include<stdio.h>
  2. int main()
  3. {
  4.     int n,arr[100];
  5.     printf("Enter the number of element(upto 100) and elements with spaces\n");
  6.     scanf("%d",&n);
  7.     for(int i=0 ; i<n ; i++){
  8.         scanf("%d",&arr[i]);
  9.     }
  10.     int check=0,i,find;
  11.     printf("Enter the number you want to find\n");
  12.     scanf("%d",&find);
  13.     for(i=0 ; i<n ; i++){
  14.         if(arr[i]==find){
  15.             check = 1;
  16.             break;
  17.         }
  18.     }
  19.     if(check){
  20.         printf("Element %d is at %d position in the array",find,i+1);
  21.     }
  22.     else printf("Not found");
  23.     return 0;
  24. }
  25. /*
  26. Output->
  27.  
  28. Enter the number of element and elements with spaces
  29. 3
  30. 44 55 66
  31. Enter the number you want to find
  32. 12
  33. Not found
  34.  
  35. Enter the number of element and elements with spaces
  36. 3
  37. 44 55 66
  38. Enter the number you want to find
  39. 66
  40. Element 66 is at 3 position in the array
  41. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement