/* factorial.c * experimenting with various storage of data with a factorial program * * King Chapter 7, Ex. 15. * * What is the largest value of n for which the factorial is correct? * Store the value of factorial in: * 1) short * 2) long * 3) long long * 4) float * 5) double * 6) long double * * Value checks done by Wolfram Alpha * * By Cory Spitzer * March 25th, 2013 */ #include int main(void){ int input; //short sum; // works for input = 7, but overflows for 8 //long sum; // works for input = 20, but overflows for 21 //long long sum; // exactly the same as above with my compiler and 64-bit arch. //float sum; // answers start to loose accuracy at input = 16, gives an // answer of 'inf' at input = 35 //double sum; // keeps accuracy until input = 14, gives 'inf' at input = 35 long double sum; // keeps accuracy until input = 26, gives 'inf' at input = 1755 printf("Enter a positive integer: "); scanf("%i", &input); sum = input; input -= 1; while (input > 1) { sum *= input; input -= 1; } printf("It's factorial is %Lf \n", sum); // change % as necessary return 0; }