RECURSIVIDAD

EJERCICOS DE FACTORIAL

F(0) =1
FACTORIAL(N)=N*FACTORIAL(N-1)
SI N>0



EJEMPLO: FACTORIAL DE (5)

FACTORIAL (5) = 5 * factorial (4) = 5 * 24 =120
FACTORIAL (4) = 4 * factorial (3) = 4 * 6 =24
FACTORIAL (3) = 3 * factorial (2) = 3 * 2 =6
FACTORIAL (2) = 2 * factorial (1) = 2 * 1 =2
FACTORIAL (1) = 1 * factorial (0) = 1 * 1 =1


ALGORITMO FACTORIAL(5):

Ejemplo 1:
Var:n,fac:entero
inicio leer 1
fac=1 n=0
mientras que 5=>n>0 haga fac(n)==n*fac(n-1) n=n+1 fin mientras que
imprimir fact(n)
terminar


Ejemplo 2:
INICIO
Var: n,cont,fac,facto:int
principla escriba ("por favor digite su #"); leer(n) fact=1 cont=1 facto=1
si n=0 { escriba("su factorial es",fact) si no
mq (cont<=n) haga facto= cont*facto escriba("su factorial es",fact)

}

Ejemplo 3:
Inicio
Var: fact,resp;
principal:
escribir("Digite el numero") leer:(fact)
resp=1
si(fact=0)
escribir("factorial=1"); sino
while(fact=>1) { resp=resp*fact fact=fact-1; }
escribir("factorial")
terminar


No hay comentarios:

Publicar un comentario