Ask Your Question
2

How can I determine and retrieve the total count of a particular number that appears in an array using C Language?

asked 2023-05-09 12:10:40 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-09 12:11:02 +0000

ladyg gravatar image

Here's an example code that counts the total number of occurrences of a particular element in an array using C Language:

#include <stdio.h>

int main() {
   int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 5};
   int count = 0;
   int numberToFind = 5;
   int i;

   for(i=0; i<10; i++) {
      if(arr[i] == numberToFind) {
         count++;
      }
   }

   printf("The total count of %d is %d\n", numberToFind, count);

   return 0;
}

In this example, we have an array arr that contains 10 elements. We want to find the total number of times the value 5 appears in the array.

We initialize a variable count to 0 and a variable numberToFind to 5. Then we loop through each element in the array and check if it's equal to numberToFind. If it is, we increment the count variable. After looping through the entire array, we print the result.

The output of this code will be:

The total count of 5 is 2
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-09 12:10:40 +0000

Seen: 10 times

Last updated: May 09 '23