Skip to content

Instantly share code, notes, and snippets.

@chihuahua
Last active December 23, 2015 20:59
Show Gist options
  • Select an option

  • Save chihuahua/6693153 to your computer and use it in GitHub Desktop.

Select an option

Save chihuahua/6693153 to your computer and use it in GitHub Desktop.
Calculates X! factorial.
/**
* 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