This C Program, using iteration, finds the occurrence for an element in an unsorted list. The user enters the element need to be counted.
Here is the source code of the C program to find the number of occurrences of a given number in a list. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program Count the Number of Occurrences of an Element in the Linked List
* without using Recursion
*/
#include <stdio.h>
int occur(int [], int, int);
int main()
{
int size, key, count;
int list[20];
int i;
printf("Enter the size of the list: ");
scanf("%d", &size);
printf("Printing the list:\n");
for (i = 0; i < size; i++)
{
list[i] = rand() % size;
printf("%d ", list[i]);
}
printf("\nEnter the key to find it's occurence: ");
scanf("%d", &key);
count = occur(list, size, key);
printf("%d occurs for %d times.\n", key, count);
return 0;
}
int occur(int list[], int size, int key)
{
int i, count = 0;
for (i = 0; i < size; i++)
{
if (list[i] == key)
{
count += 1;
}
}
return count;
}
$ gcc occurnumber.c -o occurnumber $ a.out Enter the size of the list: 10 Printing the list: 3 6 7 5 3 5 6 2 9 1 Enter the key to find it's occurence: 3 3 occurs for 2 times.
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
If you wish to look at other example programs on Linked List, go to Linked List. If you wish to look at programming examples on all topics, go to C Programming Examples.
Next Steps:
- Get Free Certificate of Merit in Data Structure I
- Participate in Data Structure I Certification Contest
- Become a Top Ranker in Data Structure I
- Take Data Structure I Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Practice Programming MCQs
- Practice Computer Science MCQs
- Apply for Data Structure Internship
- Buy Data Structure Books
- Practice Design & Analysis of Algorithms MCQ