Computer Science, asked by tk6476813, 18 days ago

write a program in Java to input a number and print the frequency of its digit

Answers

Answered by anindyaadhikari13
6

\texttt{\textsf{\large{\underline{Hint!}}}}

1. Accept number.

2. Use array to count and store frequency of digits.

\texttt{\textsf{\large{\underline{The C{o}de!}}}}

import java.util.*;

public class Frequency{

   public static void main(String s[]){

       Scanner sc=new Scanner(System.in);

       int a[]={0,0,0,0,0,0,0,0,0,0};

       System.out.print("Enter a number: ");

       int n,i;

       n=sc.nextInt();

       sc.close();

       while(n!=0){

           a[n%10]++;

           n/=10;

       }

       for(i=0;i<a.length;i++){

           if(a[i]!=0)

               System.out.println("Frequency of "+i+" is: "+a[i]);

       }

   }

}

\texttt{\textsf{\large{\underline{Sample I/O!}}}}

Enter a number: 2211335

Frequency of 1 is: 2

Frequency of 2 is: 2

Frequency of 3 is: 2

Frequency of 5 is: 1

See attachment for verification.

Attachments:
Similar questions