Advertisement
dmilicev

search_element_in_array.c

Nov 28th, 2019
276
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.82 KB | None
  1. /*
  2.  
  3.     search_element_in_array.c
  4.  
  5.  
  6.     You can find all my C programs at Dragan Milicev's pastebin:
  7.  
  8.     https://pastebin.com/u/dmilicev
  9.  
  10. */
  11.  
  12. #include <stdio.h>
  13.  
  14. int main(void)
  15. {
  16.     int i, value, n=10;
  17.     int a[10]={12,23,34,45,56,67,36,44,11,100};
  18.  
  19.     printf("\n Array is: \n");
  20.  
  21.     for(i=0; i<n; i++)
  22.         printf("\n a[%d] = %3d ",i,a[i]);
  23.  
  24.     printf("\n\n Enter value to search: ");
  25.     scanf("%d", &value);
  26.  
  27.     i=0;
  28.     while( a[i] != value && i < n ){
  29.         printf("\n a[%d] = %d ",i,a[i]);
  30.         i++;
  31.     }
  32.  
  33.     (i<n) ? printf("\n a[%d] = %d ",i,a[i]) : printf("\n");
  34.  
  35.     (i==n) ? printf("\n\n Couldn't find value %d \n",value) : printf("\n\n Entered value %d has index %d \n",value,i);
  36.  
  37. /*
  38.     if(i==n)
  39.         printf("\n\n Couldn't find value %d \n",value);
  40.     else
  41.         printf("\n\n Entered value %d has index %d \n",value,i);
  42. */
  43.  
  44.     return 0;
  45. }
Advertisement
RAW Paste Data Copied
Advertisement