I have a program called public class ZeroCounter {. I want to add a new method to it called numberOfDigits() and add a line to test it in the main() method. How should I go abo
Try this method to count digits of an integer number:-
public int numberOfZeros(int number){
return ((int) Math.log10(number) + 1);
}
Or you can try this code
//Declare input as scanner
Scanner input = new Scanner(System.in);
//Take input
System.out.println("Enter Number :");
no = input.nextInt();
System.out.println("Enter digit :");
digit = input.nextInt();
//add while loop
while(no>0)
{
m=no%10;
if(m==digit)
oc++;
no=no/10;
}
System.out.println("Digit occurred "+oc+" times");