Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define MAX_SIZE 100 // Maximum array size
- int main()
- {
- int arr[MAX_SIZE];
- int size, i, toSearch, found;
- /* Input size of array */
- printf("Enter size of array: ");
- scanf("%d", &size);
- /* Input elements of array */
- printf("Enter elements in array: ");
- for(i=0; i<size; i++)
- {
- scanf("%d", &arr[i]);
- }
- printf("\nEnter element to search: ");
- scanf("%d", &toSearch);
- /* Assume that element does not exists in array */
- found = 0;
- for(i=0; i<size; i++)
- {
- /*
- * If element is found in array then raise found flag
- * and terminate from loop.
- */
- if(arr[i] == toSearch)
- {
- found = 1;
- break;
- }
- }
- /*
- * If element is not found in array
- */
- if(found == 1)
- {
- printf("\n%d is found at position %d", toSearch, i + 1);
- }
- else
- {
- printf("\n%d is not found in the array", toSearch);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment