There's a lot wrong with it. Most variables are unnecessary. When you are creating the code you have to ask yourself why you need it. If you do not have an answer do not use. If you can not justify anything in the code do not use.
I do not understand why this parameter f
in the function, seems completely meaningless, even more that it will initialize this variable there with 1, and one of the errors is to do this within while
. But if you think hard about what you need it for? It already has a variable that determines where the count starts and can go down until it reaches where it wants. I just need one to save the multiplication results. It's all simpler. And in the data request to do the factorial only need a variable to save the required value.
#include <stdio.h>
int fatorial(int n) {
int resultado = 1;
while (n > 1) resultado *= n--;
return resultado;
}
int main(void) {
int n;
printf("Digite o numero a ser fatorado:\n");
scanf("%d", &n);
printf("%d", fatorial(n));
}
See running on ideone . And no Coding Ground . Also put it in GitHub for future reference .