Write a Java function count digits(int num); where num is a positive integer. The function counts how many times each of the digits 0..9 appears in num, and prints the results (see the following example below). Example: Calling count digits(347213); will print the following: "The number 0 appaeared 0 times in 347213.... (same with 1, 2, 3).. No helper/recursion allowed, only iteration.
import java.Math.;
public int count-digits (int num){
int count = 0;
String numF = string.valueOf(num);
// We get the number of digits by logs.
for(int j=0; j <= 9; j++){ //loop for each digits
for(int i=0; i < Math.floor(Math.log10(num)); i++){ //this loops checks each no.
if(numF.charAt(j).equals(i)){
count++;
}
return count;
count=0;
}
}
}
Two problems:
(1) How do I return the string alongside to it?
(2) Does this work? Is there a better solution?