(1) You are adding "int" to "double":
acc = acc + fat;
The problem is that high factorial values fit in double, but do not fit in int.
By the way, you’re not even using the variable "acc" for anything...
(2) You are not initiating the variable "acc":
int cc, cf, acc;
That is, even ignoring that acc should be double, and that you are not using acc for anything, your acc accounts result in undefined value, because acc has not been initialized.
(3) Your logic is inefficient and confusing:
for (cc = 0; cc <= 20; cc++)
{
if (cc == 0) // <------- INEFICIENTE, está repetindo a mesma comparação
fat = 1; // <------- 21 vezes, sendo que só 1 vez ela vai ser verdadeira
else
{
for (cf = 0; cf < cc; cf++) // <---- CONFUSO: por que este segundo loop ???
{
fat = fat * cf;
acc = acc + fat;
}
}
(4) Your program is not documented. You didn’t even post a comment explaining its logic.
Below, a simplified version of the program.
#include <stdio.h>
int main()
{
int cc;
double fat = 1;
// caso especial: 0! = 1
printf("* 0! = 1\n");
// caso geral: n! = 1 * 2 * .. * n
for (cc = 1; cc <= 20; cc++)
{
fat *= cc;
printf("* %d! = %.0lf\n", cc , fat);
} // for
return 0;
}
Testing:
$ 380272.exe
* 0! = 1
* 1! = 1
* 2! = 2
* 3! = 6
* 4! = 24
* 5! = 120
* 6! = 720
* 7! = 5040
* 8! = 40320
* 9! = 362880
* 10! = 3628800
* 11! = 39916800
* 12! = 479001600
* 13! = 6227020800
* 14! = 87178291200
* 15! = 1307674368000
* 16! = 20922789888000
* 17! = 355687428096000
* 18! = 6402373705728000
* 19! = 121645100408832000
* 20! = 2432902008176640000
$