I have posted a similar program earlier but in this program I have used array to store the digits extracted from the number and then from the array the frequency of each digit is counted.
import java.io.*;
class Digit
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,j,temp,n,x=0;
int arr[]=new int[20];
public void show() throws Exception
{
System.out.println("Enter the number:");
n=Integer.parseInt(br.readLine());
while(n > 0)
{
arr[x++]=n%10;
n=n/10;
}
for(i=0;i< x-1;i++)
{
for(j=i+1;j< x;j++)
{
if(arr[i] > arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
n=arr[0];
i=1;
for(j=1;j< x; j++)
{
if(n==arr[j])
i++;
else
{
System.out.println("Digit="+n+" Frequency="+i);
i=1;
n=arr[j];
}
}
}
public static void main(String args[]) throws Exception
{
Digit ob=new Digit ();
ob.show();
}
}
No comments:
Post a Comment