C Program to Count Occurrences of an Element in an Array

C Program to Count Occurrences of an Element in an Array

C program to count occurrences of an element in an array; Through this tutorial, we will learn how to count occurrences of an element in an array in c programs.

C Program to Count Occurrences of an Element in an Array

/* C program to find occurrence of an element 
in one dimensional array.
*/

#include <stdio.h>
#define MAX 100

int main()
{
    int arr[MAX], n, i;
    int num, count;

    printf("Enter total number of elements: ");
    scanf("%d", &n);

    //read array elements
    printf("Enter array elements:\n");
    for (i = 0; i < n; i++) {
        printf("Enter element %d: ", i + 1);
        scanf("%d", &arr[i]);
    }

    printf("Enter number to find Occurrence: ");
    scanf("%d", &num);

    //count occurance of num
    count = 0;
    for (i = 0; i < n; i++) {
        if (arr[i] == num)
            count++;
    }
    printf("Occurrence of %d is: %d\n", num, count);
    return 0;
}

The output of the above c program; as follows:

Enter total number of elements: 5
Enter array elements:
Enter element 1: 5
Enter element 2: 2
Enter element 3: 5
Enter element 4: 9
Enter element 5: 4
Enter number to find Occurrence: 5
Occurrence of 5 is: 2

Recommended C Programs

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *