kucheasysa

linear search

Jun 6th, 2023
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int size, i, key, found = 0;
  5.  
  6. printf("Enter the size of the array: ");
  7. scanf("%d", &size);
  8.  
  9. int arr[size];
  10.  
  11. printf("Enter the elements of the array:\n");
  12. for (i = 0; i < size; i++) {
  13. scanf("%d", &arr[i]);
  14. }
  15.  
  16. printf("Enter the element to search: ");
  17. scanf("%d", &key);
  18.  
  19. for (i = 0; i < size; i++) {
  20. if (arr[i] == key) {
  21. found = 1;
  22. break;
  23. }
  24. }
  25.  
  26. if (found) {
  27. printf("Element %d found at index %d\n", key, i);
  28. } else {
  29. printf("Element %d not found in the array\n", key);
  30. }
  31.  
  32. return 0;
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment