I am trying to code a function that calculates the factorial of a given number which I defined as follows:
unsigned long long int fact(unsigned long long int n){
return (n == 0 || n == 1) ? 1 : fact(n - 1) * n;
}
and my main function is:
int main(int argc, const char * argv[]) {
unsigned long long int z;
cout << "Please eneter the number: ";
cin >> z;
z = fact(z);
cout << z << endl;
return 0;
}
The code works fine until 20 but after that garbage value is returned.
I know I can solve the problem using an array but I don't understand why the program runs in such a behavior?
Aucun commentaire:
Enregistrer un commentaire