Skip to content

Instantly share code, notes, and snippets.

@mahata
Created September 18, 2011 21:25
Show Gist options
  • Save mahata/1225569 to your computer and use it in GitHub Desktop.
Save mahata/1225569 to your computer and use it in GitHub Desktop.
C++ Primer 7.5
#include <iostream>
long factorial(int n);
int main()
{
using namespace std;
int n;
cout << "Please input a number(n): ";
cin >> n;
cout << "The factorial of n is: " << factorial(n) << endl;
return 0;
}
long factorial(int n)
{
if (0 > n) { exit(-1); /* error! */ }
if (0 == n) { return 1; };
return n * factorial(n - 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment