Created
September 18, 2011 21:25
-
-
Save mahata/1225569 to your computer and use it in GitHub Desktop.
C++ Primer 7.5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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