Skip to content

Instantly share code, notes, and snippets.

@rogerioagjr
Created May 17, 2016 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogerioagjr/c56eb14c6152122b3c9c1106987a505a to your computer and use it in GitHub Desktop.
Save rogerioagjr/c56eb14c6152122b3c9c1106987a505a to your computer and use it in GitHub Desktop.
Fatorial
#include <stdio.h>
int main()
{
int fat[10], n, j, cont = 0;
fat[0] = 1;
// calculo todos os fatoriais necessários
for(int i = 1; i <= 9; i++)
fat[i] = i*fat[i-1];
// leio o valor de n
scanf("%d", &n);
//enquanto n!=0
while(n)
{
// procuro o maior fatorial que cabe em n
j = 1;
while(n >= fat[j])
j++;
// subtraio o seu valor de n
n-=fat[j-1];
// e o adiciono na contagemd a resposta
cont++;
}
// por fim, basta imprimir o valor de cont
printf("%d\n", cont);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment