115k views
5 votes
factorial(n)  int:a.this function takes one argument n as a string and returns n! (the factorial of n), if n is not a non-negativeint, return none (hint: the string method isdigit() may be useful). your factorial calculation must be based on the following formula (you are allowed to calculate the values in reverse order, but you are not allowed to simply call math.factorial(n) or similar): note: by definition 0!

User Herz Rod
by
3.3k points

1 Answer

0 votes

#include <bits/stdc++.h>

typedef int i;

i factorial(i n) {

return (n>=1) ? n*factorial(n-1) : 1;

}

i main(i argc, char* argv[]) {

i idx; std::cin>>idx;

assert(idx>=0);

std::cout << "Factorial of " << idx << " is " << factorial(idx) << std::endl;

return 0;

}

User Pia
by
3.4k points