This blog is under construction

Saturday 29 June 2013

C program to find the number of occurrence of the given number in an array

Write a C program to find the number of occurrence of the given number in an array.


  #include <stdio.h>
  #include <stdlib.h>
  int main() {
        int *data, n, val, i, count = 0;

        /* get the number of inputs */
        printf("Enter the number of inputs:");
        scanf("%d", &n);

        /* dynamically allocate memory to store entries */
        data = (int *)malloc(sizeof (int) * n);

        /* get the input entries from the user */
        printf("Enter your inputs:\n");
        for (i = 0; i < n; i++)
                scanf("%d", &data[i]);

        /* enter the input to which occurrence needs to found */
        printf("Enter your input to find no of occurrence:");
        scanf("%d", &val);

        /* find the number of occurrence */
        for (i = 0; i < n; i++) {
                if (data[i] == val)
                        count++;
        }
        printf("No of occurrence of %d in the "
                "given array is %d\n", val, count);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the number of inputs:5
  Enter your inputs:
  100 200 300 100 100
  Enter your input to find no of occurrence:100
  No of occurrence of 100 in the given array is 3



No comments:

Post a Comment