Skip to content

Instantly share code, notes, and snippets.

@shivamMg
Created February 6, 2015 10:37
Show Gist options
  • Save shivamMg/3637c7f59a07d656834f to your computer and use it in GitHub Desktop.
Save shivamMg/3637c7f59a07d656834f to your computer and use it in GitHub Desktop.
Calculate factorial of 100 in C.
#include <stdio.h>
int main(void) {
int carry,i,j,x,lenArr;
int N = 100; // Factorial of 100
int Arr[158] = {0}; // Factorial of 100 is approximately 9 * 10^157 => 157 digits
Arr[0] = 1;
lenArr = 1; // Number of elements in Arr
carry = 0;
for(i=1;i<=N;i++) {
for(j=0;j<lenArr;j++) {
x = Arr[j]*i + carry;
Arr[j] = x%10;
carry = x/10;
}
while(carry>0) {
Arr[lenArr] = carry%10;
carry = carry/10;
lenArr++;
}
}
for(i=lenArr-1;i>=0;i--)
printf("%d",Arr[i]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment