#include int main() { int num, product, i; /* Prompt user for a number and read it in */ printf("Enter number: "); scanf("%d", &num); /* One way to do it - using a while loop, work backwards */ product = 1; i = num; while(i >= 1) { product *= i; i = i - 1; /* Can also be written as "i--" or "i -= 1" */ } printf("Factorial of %d = %d\n", num, product); /* Another way to do it - using a for loop */ product = 1; for(i=1; i <= num; i++) { product *= i; } printf("Factorial of %d = %d\n", num, product); return 0; } /* Try determining factorial of 12 and then the factorial of 13. ** The latter should (of course) be 13 times bigger than the ** former, but you'll notice this is not the case. ** ** 12! = 479001600 ** 13! = 6227020800 (answer reported by this program: 1932053504) ** ** Try 17 as input - you'll notice you get a negative number out! ** ** 32 bit two's complement can represent numbers from ** -2^31 to (2^31)-1 ** i.e.-2147483648 to +2147483647 */