You are not logged in. Please login at www.codechef.com to post your questions!

×

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

asked 05 May '12, 20:38

sds1810's gravatar image

0★sds1810
1224
accept rate: 0%

edited 05 May '12, 21:29

cyberax's gravatar image

2★cyberax ♦
3.4k21955


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

link

answered 05 May '12, 21:32

cyberax's gravatar image

2★cyberax ♦
3.4k21955
accept rate: 20%

edited 05 May '12, 21:34

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

link

answered 26 Mar '16, 09:58

smitthakkar's gravatar image

3★smitthakkar
11
accept rate: 0%

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

link

answered 26 Mar '16, 11:17

kshiti1's gravatar image

0★kshiti1
1
accept rate: 0%

link

answered 02 Sep '17, 17:53

mukul_vashisht's gravatar image

4★mukul_vashisht
1
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×164

question asked: 05 May '12, 20:38

question was seen: 1,181 times

last updated: 02 Sep '17, 17:53