-3
#include <stdio.h>

main() {
    int n;
    scanf("%d", &n);
    int zz, count;
    int i = 5;
    while(zz >= 1) {
        zz = n / i;
        count += zz;
        i = i * 5;
    }
    printf("%d", count);
}

This is code to find trailing 0's in factorial of a number. It's giving different output in Ubuntu than on Windows.

Functino
  • 1,939
  • 17
  • 25
Boyka
  • 138
  • 9
  • `main()` is incorrect – phuclv Feb 13 '16 at 08:07
  • @Lưu Vĩnh Phúc The missing return type is no problem, C will assume it's `int`. The `()` is more problematic but few compilers will have a problem with it and anyway there are worse problems with this code. – Functino Feb 13 '16 at 08:43
  • 1
    @Functino not in C99. And even in the implicit case `void` is still required. [What should main() return in C and C++?](http://stackoverflow.com/q/204476/995714). The correct forms are `int main(void)` and `int main(int argc, char **argv)` – phuclv Feb 13 '16 at 08:46

4 Answers4

1

You can find out at least a few of the issues by enabling warnings during compilation. In this case (output from clang -Wall -Wextra the_file.c):

tst.c:3:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main() {
^
tst.c:10:9: warning: variable 'count' is uninitialized when used here [-Wuninitialized]
        count += zz;
        ^~~~~
tst.c:6:18: note: initialize the variable 'count' to silence this warning
    int zz, count;
                 ^
                  = 0
tst.c:6:9: warning: variable 'zz' is used uninitialized whenever function 'main' is called [-Wsometimes-uninitialized]
    int zz, count;
    ~~~~^~
tst.c:8:11: note: uninitialized use occurs here
    while(zz >= 1) {
          ^~
tst.c:6:11: note: initialize the variable 'zz' to silence this warning
    int zz, count;
          ^
           = 0
3 warnings generated.

You should fix all of those first.

viraptor
  • 33,322
  • 10
  • 107
  • 191
0

I'm not sure exactly what you're asking, but there are a couple problems with your code I can point out:

  1. Indent your code. Indenting code is mandatory.
  2. Try to put whitespace around operators. It goes a long way towards making your code more readable.
  3. Don't use scanf. You will be much happier if you avoid the entire scanf family of functions.
  4. You're using zz and count without initializing them. C does not initialize variables to any particular value; you always must initialize them yourself.
  5. You should really change main to int main. They do the same thing, but the latter is easier to read.

If you add some test cases explaining what your code is supposed to do, it will be easier to answer your question.

Functino
  • 1,939
  • 17
  • 25
0

Uninitialized variable

int zz, count;
int i = 5;
while(zz >= 1) {  // what is zz?

i = i * 5; overflows. Undefined behavior. Different result on different systems is not unexpected.

--

There is no need to compute the factorial. Just count the number of 5s in n.

#include <stdio.h>
int main(void) {
  int n;
  scanf("%d", &n);
  printf("%d", n/5);
}

15! --> 1307674368000
9! --> 362880

To have a trailing 0, the factual needs a product of some multiple of 5 and a multiple of 2. Every other number in the factorial combination is even and every 5th is a multiple of 5.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0
     public static void main(String[] args) {
        int n=23;
        String fact= factorial(BigInteger.valueOf(23)).toString();
        System.out.format("Factorial value of %d is %s\n", n,fact);
        int len=fact.length();
        //Check end with zeros
        if(fact.matches(".*0*$")){
            String[] su=fact.split("0*$");
            //Split the pattern from whole string
            System.out.println(Arrays.toString(fact.split("0*$")));
           //Subtract from the total length 
            System.out.println("Count of trailing zeros "+(len-su[0].length()));
         }

    } 

     public static BigInteger factorial(BigInteger n) {
        if (n.equals(BigInteger.ONE) || n.equals(BigInteger.ZERO)) {
            return BigInteger.ONE;
        }
        return n.multiply(factorial(n.subtract(BigInteger.ONE)));


      }
rarumugam
  • 11
  • 2