필터 지우기
필터 지우기

using the while command

조회 수: 1 (최근 30일)
francesca
francesca 2013년 5월 8일
By using the while command, I have to determine the number of terms needed so that the product 2x4x6x8...x2n exceeds a given number N (input by the user). The following is what I tried but it doesn't seem to work.
function y=question2(i,n);
N=input('Enter number N: ')
for i=1:1:n
question2=2*(i)
while question2<=N
question2=question2*(i+1)
display(n)
end
end
Thanks in advance.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 8일
편집: Azzi Abdelmalek 2013년 5월 8일
You should use only while loop (remove the for loop), then increment the variable i inside the while loop (i=i+2).
  댓글 수: 1
francesca
francesca 2013년 5월 8일
Thanks very much for your help and for your time.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

David Sanchez
David Sanchez 2013년 5월 8일
Hello Francesca, firstly, why do you use "i" as a input variable? Since you use "i" as index within the for-loop, the input "i" will not be used. (advice: try to avoid using "i" as loop index to avoid confusing with imaginary unit) Second: do not forget to add semicolons (;) at the end of each line, it will produce a cleaner result, unless you want lots of useless information on the command windows. Before attempting anything, try to determine what your input variables will be.

David Sanchez
David Sanchez 2013년 5월 8일
try this code, but remember that we are not here to do your homework.
N = input('Enter number N: ');
n = 2;
r = 2;
count = 0;
while r < N
count = count + 1;
n = n + 2;
r = 2*n;
end
disp(count)
  댓글 수: 1
francesca
francesca 2013년 5월 8일
Thanks very much for your help and for your time.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by