Skip to content

Instantly share code, notes, and snippets.

@darkf
Created December 22, 2011 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkf/1508707 to your computer and use it in GitHub Desktop.
Save darkf/1508707 to your computer and use it in GitHub Desktop.
sum digits of factorial with double in C
/* sums the digits of the factorial 10 (since not much larger can be held by a double)
copyright (c) 2011 darkf
use for anything, CC-0 or public domain etc */
#include <stdio.h>
#include <string.h>
double f(int n){return !n?1:n*f(n-1);}
int g(char* b){int x=0,i;for(i=0;i<strlen(b);i++)x+=b[i]-'0';return x;}
main(){char b[256];double x=f(10);sprintf(b,"%.0f",x);printf("%d",g(b));}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment