MATLAB: While or for loop

factorialfor loopwhile

Hi, I want to write a script which will evaluate the lowest factorial which is greater than the user's input, and I am unsure of whether to use a while or a for loop to execute this. Any help is always much appreciated! This is my code so far:
num=input('Enter a number: ');
n=0;
while (true)
n=n+1;
f(1i)=factorial(n);
if f(1i)>num
formatSpec='The factorial greater than %d is %d';
fprintf(formatSpec,num,f);
else
continue
end
end

Best Answer

  1. Use while loop
  2. instead of using if else and continue break in while(true), add condition in while loop
  3. Print after the while loop ends
PS: you need a break condition for a while(true) type loop otherwise it will continue runing forever