Help in factorial

Jan 24, 2014 at 1:32pm
Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// factorial calculator
#include <iostream>
using namespace std;

long factorial (long a)
{
  if (a > 1)
   return (a * factorial (a-1));
  else
   return 1;
}

int main ()
{
   int n;
  long number = n;
  cin>>n;
  cout << number << "! = " << factorial (number);
  return 0;
} 


when I complie it, it shows OK. But when I execute it it shows
Failed to execute C:/......prog.exe


I don't understand it
Jan 24, 2014 at 2:35pm
try this...

1
2
3
4
 
  long number;
  cin>>number;
  cout << number << "! = " << factorial (number);
Jan 24, 2014 at 2:39pm
when i used

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// factorial calculator
#include <iostream>
using namespace std;

long factorial (long a)
{
  if (a > 1)
   return (a * factorial (a-1));
  else
   return 1;
}

int main ()
{
   int n;
  long number = 2;
  cout << number << "! = " << factorial (number);
  return 0;
}


it worked

thanks.. i will try it for sure.. but what is the reason of "Failed to execute "
Jan 24, 2014 at 3:25pm
Failed to execute
Where does it show execution failed it works fine
Jan 24, 2014 at 3:29pm
in the output...

Sharan123
Jan 24, 2014 at 5:08pm
Sorry doesn't show any execution failure in my one does it show problems in any other codes
Jan 24, 2014 at 5:31pm
yes... almost all..but some shows result after trying many times
Jan 24, 2014 at 8:23pm
You have ignored Jaybob66's response, which is correct. You aren't using n. Get rid of n. Only use number.

[edit]
While I'm at it, you should make sure that it can handle negative numbers. (Because right now it cannot.)
Last edited on Jan 24, 2014 at 8:24pm
Topic archived. No new replies allowed.