why does this factorial program does not return the number of trailing zeroes for 25! as 6?

http://www.codechef.com/viewsolution/1017061

when I give input as follows:

5
5
10
15
20
25

the output I get is:

1
2
3
4
0


    import java.util.Scanner;
class Factorial
{
	public static void main(String args[])
	{
		int i,n;
		Scanner sc= new Scanner(System.in);
		n=sc.nextInt();
		long a[]=new long[n];
		long cnt[]=new long[n];
		for(i=0;i<n;i++)
		{	
			a[i]=fact(sc.nextLong());
			cnt[i]=0;
			while(a[i]%10==0)
			{
				cnt[i]++;
				a[i]=a[i]/10;
			}
		}
		for(i=0;i<n;i++)
			System.out.println(cnt[i]);	
	}
	 public static long fact(long n)
    {
        if (n == 0) return 1;
        else
        	return n * fact(n-1);
    }
}

when I give input as follows:

5
5
10
15
20
25

the output I get is:

1
2
3
4
0

does 25! (=15511210043330985984000000) fit in a long in Java ?

1 Like

@sds1810 : this is not the correct way of solving it…you should look carefully what factors contribute to making a zero in a factorial :slight_smile:

may be some precision problem is present
check it!!!

Refer to: