1.

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

Answer»

\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

           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.



Discussion

No Comment Found