Untitled
#include <stdio.h> // Function to search for a number in an array int search(int arr[], int size, int target) { int i; for (i = 0; i < size; i++) { if (arr[i] == target) { return i; // Return the index of the target element } } return -1; // Return -1 if the target element is not found } int main() { int arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}; int size = sizeof(arr) / sizeof(arr[0]); int target; printf("Enter a number to search for: "); scanf("%d", &target); int result = search(arr, size, target); if (result != -1) { printf("Number found at index %d\n", result); } else { printf("Number not found in the array\n"); } return 0; }
Leave a Comment