Last active
May 13, 2019 00:24
-
-
Save dstein64/97220e16b9e316cf4f8a0b50229408aa to your computer and use it in GitHub Desktop.
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 <assert.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
/* | |
* Calculates factorial. | |
* Overflow is not checked/handled. | |
*/ | |
int factorial(int x) { | |
assert(x >= 0); | |
int result = 1; | |
for (int i = 1; i <= x; ++i) { | |
result *= i; | |
} | |
return result; | |
} | |
int main(int argc, char* argv[]) { | |
assert(argc == 2); | |
int x = atoi(argv[1]); | |
int result = factorial(x); | |
printf("%d\n", result); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment