Debugging help about FCTRL

I am trying the FCTRL exercise and am getting some good results for the first couple of tests after this mine results fall behind. For 60 I get 14 trailing zeroes for 100 I get 24 but for 1024 I get 245 and for 23456 I get 5666.

Here is my code:

public class TrailingZeroesMultipleOfFive {

public static void main(String[] args) {
	int N;
	int factor = 5;
	int i = 1;
	int sum = 0;
	//get input from console
	//Scanner scan = new Scanner(;
	//N = scan.nextInt();
	N = 23456;
	while(N >= 1){
		N /= factor; //divide by factor 
		sum += N;		//add the division result to sum
		factor = (int) Math.pow(factor,i++); //next loop N will be divided by the next power of 5

Do you think the problem is in the pow function and the casting?

my ans is 125

No, the problem is not in pow function and casting. Your approach is almost correct except the statement factor = (int) Math.pow(factor,i++);. This statement is not needed because the factor will be always 5, you do not need to change this value.

See your code modified and ACcepted here.

@mediocoder you are my angle over here! I was following the mathematical logic straight, meaning that I was looking for all numbers that I get by factoring 5 out and then 5^2 and 5^3 , 5^n until the number after the division is less than 1.

1 Like

Not a big deal, thanks for the appreciation. Good logical approach.