Help in factorial

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
try this...

1
2
3
4
 
  long number;
  cin>>number;
  cout << number << "! = " << factorial (number);
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 "
Failed to execute
Where does it show execution failed it works fine
in the output...

Sharan123
Sorry doesn't show any execution failure in my one does it show problems in any other codes
yes... almost all..but some shows result after trying many times
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
Topic archived. No new replies allowed.