To count the occurrence of given number in an array by traversing the array and comparing each element with number. If number matches increment the counter and print the counter, if counter value greater than 1, print the message otherwise print number not found in array.
Count occurrence in array
1 #include <stdio.h>
2
3 int main() {
4 int i, num, arr[10], count = 0;
5
6 for(i = 0; i < 10; i++) {
7 printf("Enter numbers for array : ");
8 scanf("%d", &arr[i]);
9 }
10
11 printf("\n Enter number to search in Array :");
12 scanf("%d", &num);
13
14 for(i = 0; i < 10; i++) {
15 if(arr[i] == num) {
16 count++;
17 }
18 }
19
20 if(count > 0) {
21 printf("%d is found in array %d times !", num, count);
22 } else {
23 printf("%d is not found in array !", num);
24 }
25
26 return 0;
27 }
In the above example, we are accepting array elements from user and accepting number which needs to be search and find how much occurrence found in an array.
An array traverse and comparing each element with given number. If number found, we are incrementing the counter number until array entire array gets traversed. A if statement checks, if count variable greater than 0, element found in array and print number with count. Otherwise print number not found in array.
1 Enter numbers for array : 1
2 Enter numbers for array : 2
3 Enter numbers for array : 5
4 Enter numbers for array : 2
5 Enter numbers for array : 3
6 Enter numbers for array : 2
7 Enter numbers for array : 5
8 Enter numbers for array : 6
9 Enter numbers for array : 2
10 Enter numbers for array : 8
11 Enter number to search in Array :2
12 2 is found in array 4 times !
1 Enter numbers for array : 1
2 Enter numbers for array : 2
3 Enter numbers for array : 3
4 Enter numbers for array : 4
5 Enter numbers for array : 5
6 Enter numbers for array : 6
7 Enter numbers for array : 7
8 Enter numbers for array : 8
9 Enter numbers for array : 9
10 Enter numbers for array : 10
11 Enter number to search in Array :15
12 15 is not found in array !
Count character occurrence
Count character occurrence
1 #include <stdio.h>
2
3 int main() {
4
5 char arr[] = {'a', 'e', 'i', 'o', 'u', 'e', 'y', 'w', 'i', 'v', 'o'};
6 char ch;
7 int i, n = 0;
8
9 printf("Enter string to reverse : ");
10 scanf("%c", &ch);
11
12 for(i = 0; i < 11; i++) {
13 if(ch == arr[i]) {
14 n++;
15 }
16 }
17
18 if(n == 0) {
19 printf("A character '%c' not present in an array.", ch);
20 } else {
21 printf("A character '%c' presents %d times in an array", ch, n);
22 }
23
24 return 0;
25 }
In the above example, a character array declared and initialized that includes duplicate characters. A character accepted from user and search using for loop. If current index element matches with entered character, a counter value will be increased by 1. If counter value is equal to 0, an entered character does not presents in an array, otherwise it will print number of occurrence in an array.
1 Enter string to reverse : i
2 A character 'i' presents 2 times in an array
3
4 Enter string to reverse : a
5 A character 'a' presents 1 times in an array
6
7 Enter string to reverse : b
8 A character 'b' not present in an array.
Related options for your search