Ingrese un número y obtenga su factorial.
Solución
Para solucionar el problema del factorial, se resolverá de forma recursiva.
Ejemplo:
- Entrada = 5
5 * 4 * 3 * 2 * 1 * 0
- Resultado = 120
#include <stdio.h>
int factorial(int X, int N);
int main()
{
int X,N;
printf("Ingrese un numero : \n");
scanf("%d",&N);
X=factorial(X,N);
printf("%d",X);
return 0;
}
int factorial(int X, int N){
if(N>0){
X=factorial(X,N-1);
X=X*N;
}else{
X=1;
}
return X;
}
0 comentarios:
Publicar un comentario