/* unfact.c - unfactorial a number Warren M Myers author http://warrenmyers.com Written: 24 Oct 05 This code is available under the public domain. */ #include void unbang(unsigned int f){ int k = 1; int m = 0; unsigned int h = f; // 1 is the factorial result of 1 or 0, but assume 1 if(1!=f) while(f>1){ m = f%k; if(m){ // if there's a modulus, it can't be a factorial printf("The number %d is not a factorial.\n",h); return; } f = f/k; k++; } k--; if(1==h) k++; printf("The number %d is the factorial of %d.\n",h,k); } int main(int argc, char** argv){ unsigned int f; char n[15] = {0}; if(argc>1){ f = atoi(argv[1]); } else{ printf("Enter a number to be unfactorialized: "); scanf("%s",n); if(n[14]) // try to avoid input weirdness n[14] = 0; f = atoi(n); } if(!f){ // 0! is defined as 1, so 0 can't be a factorial printf("0 is not a factorial.\n"); return; } unbang(f); return 0; }