Question
Find frequency of digits in a number using recursion
ENTER THE NUMBER
123456789123456789
Frequency of digits
0:0
1:2
2:2
3:2
4:2
5:2
6:2
7:2
8:2
9:2
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class frequencyOfDigits
{
public static long CountDigits(long num,int digit,long noOfDigits)
{
if(num==0)
{
return noOfDigits;
}
else
{
if(num%10==digit)
{
noOfDigits++;
}
return CountDigits(num/10,digit,noOfDigits);
}
}
public static void main()
{
int i=0;
long num=0,count=0;
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE NUMBER");
num=sc.nextLong();
System.out.println("Frequency of digits");
for(i=0;i<=9;i++)
{
count=CountDigits(num,i,0);
System.out.println(i+":"+count);
}
}
}