Help with the Z function (factorial)

Hey, I have been programming for a while now and have made a simple program to find the Z function of a number, which counts the number of zeros obtained at the end of a factorial of a certain number. For example, 3628800 is the factorial of 10, so the Z would be 2.
The program works fine up to about 17, where the factorial jumps down to negatives and messes up the program. Also, after a a while, everything falls to zero, leaving you with a final result of 1 for any input higher than 33.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <conio.h>
#include <math.h>
#include <sstream>

using namespace std;
string convertInt(int number)
{
   stringstream ss;//create a stringstream
   ss << number;//add number to the stream
   return ss.str();//return a string with the contents of the stream
}
void z(int numorig)
{

    cout << (numorig) << " ";
    string num=(convertInt(numorig));//these two lines are for testing
    cout << (num) << " ";//testing as well
    cout << (num.length() - num.find_last_not_of("0")-1);

}

int factorial(int factnum)
{
    int factorial=0;
    for(int i = 0;i<=factnum;i++)
    {
        if(i==0)
        {
            factorial = 1;
        }
        else
        {
            factorial=(factorial*i);
            cout << (factorial) << ", " << (i) << "\n";
        }
    }

    return (factorial);

}

int main()
{
    int whichnum;
    cin >> (whichnum);
    z(factorial(whichnum));
    return 0;
}

Please help me!
you can only hold so much data in ints, try an 'unsigned long long int', but even that has a limit
yeah, the glitches start at 20 now and the sequence drops to zero at 65. thanks.
i believe Z should be
Z=floor(whichnum/5.0);
sinze you only add an extra 0 when you multiplie by 5 or 10.
Last edited on
Topic archived. No new replies allowed.