MATLAB: For and while loops

loopsMATLAB

Hi everyone,
How can I write a Matlab function that takes an input integer n and computes the following
n!=n×(n1)×(n2)×2×1 by using:
The while loop and name it getFacWhile(n).
The for loop and name it getFacFor(n).

Best Answer

function F = getFacWhile(n)
F=1;
while n>1
F=F*n;
n=n-1;
end
end
function F = getFacWhile(n)
F=1;
for i=1:n
F=F*i;
end
end
Related Question