Code for calculating number of trailing zeroes after factorial While trying my hands at the beginner codechef problem I came across this . So basically doing that problem with Dynamic programming is an overkill but why not try that and get some DP practice.

Nice, I almost solved that problem in minutes but the problem is this takes too much memory , and ends up with a Segmentation Fault upon entering a big integer , I solved this problem by running a  ulimit -s unlimited in my local machine but that wont make the codechef judge happy.

Here is the code:

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;


int getZeroes(long long n){
    if(n&gt;0){
        if(n%5 == 0){
            long long a = n/5;
            int b = 1;
            while(a/5){
                if(a%5 == 0){
                    b++;
                }else{
                    break;
                }
                a = a/5;
            }
            return (b+getZeroes(n-1));
        }else{
            return (getZeroes(n-1));
        }
    }
}

int main(){
    int n;
    std::cin &gt;&gt; n;
    std::vector&lt;int&gt; x;
    for(int i=0;i&lt;n;i++){
        long long cases;
        std::cin &gt;&gt; cases;
        x.push_back(getZeroes(cases));
    }

    for(int itr : x){
        std::cout &lt;&lt; itr &lt;&lt; std::endl;
    }


return 0;
}


Upon some research I found most recommend to declare the items in a heap rather than stack , but that's somewhat confusing to me.  Can anyone help me to find out what can I do to overcome this?
Code Review
All return paths from a function must return  value. Leaving a function that has a non void type without a return is undefined behavior (google nasal dragons).
int getZeroes(long long n){
    if(n&gt;0){
       // STUFF
       return stuff;
    }
    // BUT HERE there is no return.
    // Any negative value or zero causes undefined behavior.
}

This while loop seems overly cumbersome with a break;
        while(a%5){
            if(a%5 == 0){  // will this ever be true.
                // STUFF   // Since it is just after a test that checks the opposite?
            }else{
                break;
            }
            a = a/5;
        }

This looks exactly the same both sides of the else:
        return (b+getZeroes(n-1));
    }else{
        return (getZeroes(n-1));

The only difference is b (which just needs to be made zero for the second case then) you can yank it out of the conditional.
Why store the results?
x.push_back(getZeroes(cases));

The only thing you do with them is print them. May as well just print them as you generate them!