ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Home » C programs » C One-D Array programs

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
/*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
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.