fork download
  1. #include <stdio.h>
  2.  
  3. #define MAX_SIZE 100 // Maximum array size
  4.  
  5. int main()
  6. {
  7. int arr[MAX_SIZE];
  8. int size, i, toSearch, found;
  9.  
  10. /* Input size of array */
  11. printf("Enter size of array: ");
  12. scanf("%d", &size);
  13.  
  14. /* Input elements of array */
  15. printf("Enter elements in array: ");
  16. for(i=0; i<size; i++)
  17. {
  18. scanf("%d", &arr[i]);
  19. }
  20.  
  21. printf("\nEnter element to search: ");
  22. scanf("%d", &toSearch);
  23.  
  24. /* Assume that element does not exists in array */
  25. found = 0;
  26.  
  27. for(i=0; i<size; i++)
  28. {
  29. /*
  30.   * If element is found in array then raise found flag
  31.   * and terminate from loop.
  32.   */
  33. if(arr[i] == toSearch)
  34. {
  35. found = 1;
  36. break;
  37. }
  38. }
  39.  
  40. /*
  41.   * If element is not found in array
  42.   */
  43. if(found == 1)
  44. {
  45. printf("\n%d is found at position %d", toSearch, i + 1);
  46. }
  47. else
  48. {
  49. printf("\n%d is not found in the array", toSearch);
  50. }
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0.02s 12404KB
stdin
5

89
87
65
54
11
stdout
Enter size of array: Enter elements in array: 
Enter element to search: 
0 is not found in the array