1 / 2
Jun 2017

I think that it is well known approach (i remember similar thing in example to recursive function)
Examples from spoj work fine but something is wrong apparently in test.

include

using namespace std;

int smfact(int a);

int main()
{
int a;
cin >> a;
for (int i = 0; i<a; i++)
{
int x;
cin >> x;
int iloczyn;
iloczyn = smfact(x);
cout << iloczyn << endl;
}

return 0;

}

int smfact(int a)
{
if (a == 1) return a;
return a*smfact(a - 1);
}

  • created

    Jun '17
  • last reply

    Dec '22
  • 1

    reply

  • 774

    views

  • 2

    users

5 years later

Your recursive solution looks good to me. Here is my solution in C
int getFactorial(int N){
if(N <= 1){
return 1;
}
return N * getFactorial(N - 1);
}
Maybe the value of factorial is larger than the size of an integer, try using long to store the factorial value.