How to write a proper algorithm in C that determines the maximum value for which the program can calculate factorial using unsigned long long int
?
My example does not work properly. Using Linux/64bit and GCC, it gives me 65 iterations.
#include <stdio.h>
int main() {
unsigned long long int num, i;
unsigned long long int factorial = 1;
num = 1;
for (i = 1; i <= num; i++) {
factorial = factorial * i;
num++;
if (factorial <= 0) {
printf("\nMaximum number is: %d\n", i - 1);
break;
}
}
}