Occurrence of number in Array in c
Occurrence of number in Array in C
Write a program to find the occurrence of an element in array. And also write a program for each elements of occurrence number display in output.
#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
Program:
#include<stdio.h>
#include<conio.h>
#define MAX 20
void frequency(int [],int);
int main()
{
int a[MAX],n,i;
printf("Enter limit of array: ");
scanf("%d",&n);
printf("\nEnter array: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
frequency(a,n);
return(0);
}
void frequency(int a[],int n)
{
int i,j,k,num,c=0;
for(i=0;i<n;i++)
{
num=a[i];
c=1;
for(j=i+1;j<n;j++)
{
if(a[j]==num)
{
c++;
for(k=j;k<n;k++)
{
a[k]=a[k+1];
}
n--;
j--;
}
}
printf("\nthe frequency of element %d is %d",a[i],c);
}
}
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.