Skip to content

Instantly share code, notes, and snippets.

@dstein64
Last active May 13, 2019 00:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
#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