clinically-proven

C Program

Feb 5th, 2021
1,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3.  
  4. #define MAX_SIZE 100  // Maximum array size
  5.  
  6. int main()
  7. {
  8.     int arr[MAX_SIZE];
  9.     int size, i, toSearch, found;
  10.  
  11.     /* Input size of array */
  12.     printf("Enter size of array: ");
  13.     scanf("%d", &size);
  14.  
  15.     /* Input elements of array */
  16.     printf("Enter elements in array: ");
  17.     for(i=0; i<size; i++)
  18.     {
  19.         scanf("%d", &arr[i]);
  20.     }
  21.  
  22.     printf("\nEnter element to search: ");
  23.     scanf("%d", &toSearch);
  24.  
  25.     /* Assume that element does not exists in array */
  26.     found = 0;
  27.    
  28.     for(i=0; i<size; i++)
  29.     {
  30.         /*
  31.          * If element is found in array then raise found flag
  32.          * and terminate from loop.
  33.          */
  34.         if(arr[i] == toSearch)
  35.         {
  36.             found = 1;
  37.             break;
  38.         }
  39.     }
  40.  
  41.     /*
  42.      * If element is not found in array
  43.      */
  44.     if(found == 1)
  45.     {
  46.         printf("\n%d is found at position %d", toSearch, i + 1);
  47.     }
  48.     else
  49.     {
  50.         printf("\n%d is not found in the array", toSearch);
  51.     }
  52.  
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment