C Program to Find Occurrence of Element in Array

Write C Program to Find Occurrence of Element in Array

// C Program to Find Occurrence of Element in Array

#include<stdio.h>
int main()
{
	int A[] = {1, 1, 222, 3, 5, 6, 8, 5, 1};

	int k, i, n;
	int flag = 0;
	int count = 0;
	n = sizeof(A) / sizeof(A[0]);

	printf("Array A :--> ");
	for(i=0; i<n; i++)
        printf("%d  ", A[i]);

	printf("\nEnter Element to Search :--> ");
	scanf("%d", &k);

	for(i = 0; i < n; i++)
	{
		if(A[i] == k)
		{
			flag = 1;
		 	count++;
		}
	}

	if(flag == 0)
	{
		printf("\nThe element is not present ");
	}
	else
	{
		printf("\nThe element is present having count : %d",count);
	}

	return 0;
}

Output:

Run 1:
Array A :--> 1  1  222  3  5  6  8  5  1
Enter Element to Search :--> 1

The element is present having count : 3

Run 2:
Array A :--> 1  1  222  3  5  6  8  5  1
Enter Element to Search :--> 2


The element is not present

≪ Previous | Next ≫