fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int n, i, key, found = 0;
  5.  
  6. printf("Enter the number of elements: ");
  7. scanf("%d", &n);
  8.  
  9. int arr[n];
  10. printf("Enter %d elements:\n", n);
  11. for (i = 0; i < n; i++) {
  12. scanf("%d", &arr[i]);
  13. }
  14.  
  15. printf("Enter the element to search: ");
  16. scanf("%d", &key);
  17.  
  18. for (i = 0; i < n; i++) {
  19. if (arr[i] == key) {
  20. printf("Element %d found at position %d (Index %d).\n", key, i + 1, i);
  21. found = 1;
  22. break;
  23. }
  24. }
  25.  
  26. if (!found) {
  27. printf("Element %d not found in the array.\n", key);
  28. }
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0s 5320KB
stdin
30 60 40 50 80

50
stdout
Enter the number of elements: Enter 30 elements:
Enter the element to search: Element 0 found at position 9 (Index 8).