Created
September 25, 2013 00:11
-
-
Save chihuahua/6693143 to your computer and use it in GitHub Desktop.
Calculates X! factorial.
This file contains hidden or 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
| /** | |
| * factorial.c | |
| * Calculates X! factorial. | |
| * | |
| * @author Chi Zeng ([email protected]) | |
| * Sep. 24, 2013 | |
| */ | |
| #include <stdio.h> | |
| int factorial(int n) { | |
| if (n < 2) { | |
| // Our base case: 0! and 1! are 1. | |
| return 1; | |
| } | |
| return n * factorial(n - 1); | |
| } | |
| // Testing: lets find out what 5! is. | |
| int main() | |
| { | |
| int n = 5; | |
| int nFactorial = factorial(5); | |
| printf("%d! is %d.", n, nFactorial); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment