Finding integers of factorial between two user-input values

3 visualizaciones (últimos 30 días)
Teresa Schwirtlich
Teresa Schwirtlich el 4 de Oct. de 2015
Respondida: Walter Roberson el 4 de Oct. de 2015
Hi all, I'm really frustrated by this problem. I'm supposed to find all integers which n! is between i and j, and I can't use the factorial function in Matlab. My script can only have one loop.
I can't seem to understand this at all.
i = input('Input value for i:');
j = input('Input value for j:');
fact=1;
n=0;
for n=1:n
fact=fact*n;
n=n+1;
if i<fact && j>fact
fprintf('Smallest n where n! is between %d and %d is %d\n',i,j,n)
end
end
What am I doing wrong and how do I fix this?

Respuestas (1)

Walter Roberson
Walter Roberson el 4 de Oct. de 2015
Your code
n=0;
for n=1:n
is equivalent to
for n=1:0
which is going to end immediately.
You are going to need to switch to a "while" loop, and the condition to exit the while is that your factorial has become larger than j.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by