using the while loop function

I am new to MATLAB and trying to use a while function and not understanding why there is no error, but keeps the system busy with no result.
function fac=fact(n);
fac=1;
while n>0
fac=fac*n;
n-1;
end

 Respuesta aceptada

Sean de Wolski
Sean de Wolski el 7 de Oct. de 2013

1 voto

You're close!
Inside the loop you say:
n-1
This just does the computation but does not assign this value back to n. You want:
n = n-1;
Which will reduce the value of n.

1 comentario

Derick
Derick el 7 de Oct. de 2013
Thank you, it was racking my brain why it looked why it would work but didn't.

Iniciar sesión para comentar.

Más respuestas (1)

Akshay Kanoria
Akshay Kanoria el 7 de Oct. de 2013

1 voto

Hi, Inside the loop you should use n = n-1, it should solve your problem.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 7 de Oct. de 2013

Respondida:

el 7 de Oct. de 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by