Created
May 17, 2016 02:49
-
-
Save rogerioagjr/c56eb14c6152122b3c9c1106987a505a to your computer and use it in GitHub Desktop.
Fatorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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