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

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

C One-Dimensional Array Programs »



Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.