Q:

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

0

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

Example:

    Input array elements:
    10, 10, 20, 30, 10
    Element to find occurrence: 10

    Output:
    Occurrence of 10 is: 30

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

/* 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;
}

Output:

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

One Dimensional Array Programs / Examples in C programming language

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to sort a one dimensional array in ascen... >>
<< C program to swap adjacent elements of a one dimen...