P060.c (1299B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 13 Dec 2025 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* write a C program to count the occurrences of a given element in an array using a user-defined 9 function. Create a function int count_occurrences(int arr[], int n, int target) that countts how 10 many times target appears in the array. */ 11 12 #include <stdio.h> 13 14 int count_occurrences(int[], int, int); 15 void inputarr(int[], int); 16 17 int main() 18 { 19 int n, arr[20], target, found; 20 printf("Enter the n (Max: 20): "); 21 scanf("%d", &n); 22 inputarr(arr, n); 23 printf("Enter the target: "); 24 scanf("%d", &target); 25 found = count_occurrences(arr, n, target); 26 if (found) 27 { 28 printf("\n%d found %d times.", target, found); 29 } 30 else 31 { 32 printf("\n%d is not found.", target); 33 } 34 return 0; 35 } 36 37 void inputarr(int arr[], int n) 38 { 39 int i; 40 for (i = 0; i < n; i++) 41 { 42 printf("Enter element %d: ", i + 1); 43 scanf("%d", &arr[i]); 44 } 45 } 46 47 int count_occurrences(int arr[], int n, int target) 48 { 49 int count = 0; 50 int i; 51 for (i = 0; i < n; i++) 52 { 53 if (arr[i] == target) 54 { 55 count++; 56 } 57 } 58 return count; 59 }