write a program in Java to input a number and print the frequency of its digit
Answers
Answered by
6
1. Accept number.
2. Use array to count and store frequency of digits.
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]);
}
}
}
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