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

1 Comment

Guillaume
Guillaume on 30 Apr 2020
Typically, you'd use a while loop if you don't know in advance how many times you're going to loop, and a for loop if you do. Therefore in your case, a while loop would be more appropriate.
However, note that you can always convert one to the other.

Sign in to comment.

 Accepted Answer

Mehmed Saad
Mehmed Saad on 30 Apr 2020

0 votes

  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

7 Comments

Thank you for your response Mehmed, is this correct?
num=input('Enter a number: ');
n=0;
while (true)
n=n+1;
f(1i)=factorial(n);
if f(1i)>num
break
else
continue
end
end
formatSpec='The factorial greater than %d is %d';
fprintf(formatSpec,num,f);
Mehmed Saad
Mehmed Saad on 30 Apr 2020
Edited: Mehmed Saad on 30 Apr 2020
Type 1i in command window
1i
ans =
0.0000 + 1.0000i
if you want to index f pass n to it and not 1i
f(n) and not f(1i)
Also now try to remove continue break and if else from the code and insert condition in
while(condition_at_which_loop_should_break)
Mehmed, I appreciate you taking the time to help me with this. Is this correct now?
num=input('Enter a number: ');
n=0;
while (f(n)>num)
n=n+1;
f(n)=factorial(n);
break
end
formatSpec='The factorial greater than %d is %d';
fprintf(formatSpec,num,f);
Rik
Rik on 30 Apr 2020
If you set a break point in your code you can step through your code step by step. Also, running this code is a faster test to see if it works than posting it and waiting for a reply.
Think about your code structure. That loop will continue while some condition is true, hence the name. Why did you need break previously? (hint: true will always be true, but f(n)>num might not)
Mehmed Saad
Mehmed Saad on 30 Apr 2020
Edited: Mehmed Saad on 30 Apr 2020
  1. break is not required
  2. intialize f before while loop (is it necessary to index f, if not replace f(n) with f in your complete code)
  3. While loop will check the condition before executing, so it will check f(n)>num when f is not defined error will occur. Now suppose f exist and you defined it as 1, then it will check f(n)>num now n is zero. But there are no such index as zero in MATLAB so error will occur. we define n = 1 so that while loop can check the condition.
  4. while loop will break when the condition is false, and continue runing if condition is true. our condition is f(n)>num and f(n) = 1 and suppose num is 10. Now 1>10, answer is false. while loop will not run because the condition is false. What if we reverse the condition f(n)<=num i.e. while loop will keep runing untill f(n) is less than equal to num. when f(n) is greater than num while loop will break automatically
  5. You have to print the last value of f and not the entire f values as f is an array so either put f(end) or f(n) in fprintf
Thankyou very much Mehmed, your explanation was extremely informative and helpful. I realise the solution is a lot simplier than I initally thought. I got the code working.
num=input('Enter a number: ');
n=1;
while (f<=num)
n=n+1;
f=factorial(n);
end
formatSpec='The factorial greater than %d is %d';
fprintf(formatSpec,num,f);
Mehmed Saad
Mehmed Saad on 30 Apr 2020
Edited: Mehmed Saad on 30 Apr 2020
Cheers.
Also when you ve problem in code apply break point approach first to detect problem as told by Rik.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!