C Programming Code Examples C > Arrays and Matrices Code Examples C Program to Read an Array and Search for an Element C Program to Read an Array and Search for an Element /* C program accept an array of N elements and a key to search. If the search is successful, it displays "Successful Search". Otherwise, a message "UNSUCCESSFUL SEARCH" is displayed. */ #include <stdio.h> void main() { int array[20]; int j, low, mid, high, key, size; printf("Enter the size of an array\n"); scanf("%d", &size); printf("Enter the array elements\n"); for (j = 0; j < size; j++) { scanf("%d", &array[j]); } printf("Enter the key\n"); scanf("%d", &key); /* search begins */ low = 0; high = (size - 1); while (low <= high) { mid = (low + high) / 2; if (key == array[mid]) { printf("Successful Search\n"); return; } if (key < array[mid]) high = mid - 1; else low = mid + 1; } printf("Unsuccessful Search\n"); }