 ### Program 261: Program to print Frequency of digits in an Array

Program 261: Program to print Frequency of digits in an Array

```#include<stdio.h>
main()
{
int i,n,num,k,frequency,flag=0;
for(i=0;i<10;i++)
{
frequency[i]=0;
}
printf("Enter number of elements in an array\n");
scanf("%d",&n);
int a[n];
printf("Enter numbers\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
//for incrementing occured number
num=a[i];
while(num>0)
{
k=num%10;
frequency[k]++;
num=num/10;
}
}
for(i=0;i<10;i++)
{
if(frequency[i]!=0)
{
flag=1;
printf("%d occured %d times\n",i,frequency[i]);
}
}
if(flag==0)
{
printf("No elements repeated\n");
}
} ```
`To know the frequency of each character in senetence`
Explanation:

1. The Program Starts with intializing
• i,k,num→ Temporary variables
• n→ To store number of elements
• frequency → To store frequency of digits
• flag used as boolean variable
• a → To store Array elements
2. ```for(i=0;i<10;i++)
{
frequency[i]=0;
}```
By defaults all the frequency of digits are assigned to zero like frequency,frequency...=0
3. ``` printf("Enter number of elements in an array\n");
scanf("%d",&n);```
To Take how many elements user is going to enter as input.
4. ```for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
//for incrementing occured number
num=a[i];
while(num>0)
{
k=num%10;
frequency[k]++;
num=num/10;
}
}```
While taking input we are storing the number in dummy variable 'num' and splitting each digit using while loop from which we are calculating frequency of digits.For Example:
• We Entered 2556 then, num=256.Lets run the while loop and check how its doing this
• Iteration 1: As num=2556 and 2556>0,which is true so while loop executes
• k=2556%10=6
• frequency[k]++→ frequency++→ frequency=1
• num=num/10→ 2556/10=255,So num=255 Now.
• Finally frequency=1,num=255
• Iteration 2: num=255 and 255>0,which is true so while loop executes
• k=255%10=5
• frequency[k]++→ frequency++→ frequency=1
• num=num/10→ 255/10=25,So num=25.
• Finally frequency=1,frequency=1,num=25
• Iteration 3: num=2 and 2>0,which is true so while loop executes
• k=2%10=2
• frequency[k]++→ frequency++→ frequency=1
• num=num/10→ 2/10=0,So num=0.(remember num is integer not float)
• Finally frequency=1,frequency=2,frequency=1,num=0
• Iteration 3: num=0 and 0>0,which is false so while loop terminates
• Finally we got frequency of first Number 2556 and the frequency is
frequency=1,frequency=2,frequency=1
5. ```for(i=0;i<10;i++)
{
if(frequency[i]!=0)
{
flag=1;
printf("%d occured %d times\n",i,frequency[i]);
}
}```
Finally if frequency of a particular digit is not zero (which was assigned to zero by default) is printed as 6 occured 1 times,5 occured 2 times,2 occured 1 times

Output: 