Skip to content

Instantly share code, notes, and snippets.

@dstein64
Last active May 13, 2019 00:24
Show Gist options
  • Save dstein64/97220e16b9e316cf4f8a0b50229408aa to your computer and use it in GitHub Desktop.
Save dstein64/97220e16b9e316cf4f8a0b50229408aa to your computer and use it in GitHub Desktop.
#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