How to use While loop

조회 수: 3 (최근 30일)
Vy Do
Vy Do 2020년 9월 11일
댓글: Vy Do 2020년 9월 12일
I have this problem "Suppose you start multiplying the numbers 1.1, 1.2, 1.3, 1.4,1.5, ... together. Use a while loop to determine how many of these numbers you have to multiply in this way to get a product that is greater than 100000." This is the code I have so far but the answer was wrong. I know that there's something wrong with the line y=y*(y+0.1) but I don't know how to express the multiplication of 1.1*1.2*1.3 and so on. Could you please help me with this?
This is my code
y = 1.1;
c = 0;
while y < 100000
y = y*(y+0.1);
c = c+1;
end

채택된 답변

Stephen23
Stephen23 2020년 9월 11일
편집: Stephen23 2020년 9월 11일
You need to keep the running total and the current value of y separate, e.g.:
y = 1.1;
t = y;
c = 1;
while t<1e5
c = c+1;
y = y+0.1;
t = t*y;
end
Independent check:
>> V = 1.1:0.1:9;
>> find(cumprod(V)>1e5,1,'first')
ans = 19
  댓글 수: 2
Vy Do
Vy Do 2020년 9월 12일
Thank you. 19 is the right answer. However, my c at the end is 99990 if I use the same code you have. In this case, my professor wants the c will be the value answer so I don't know if we can fix this a little bit so that it will return c=19 for answer?
Vy Do
Vy Do 2020년 9월 12일
Sorry, you're right. I know where my code went wrong, instead of t<1e5 for the while condition, I typed in y. Thart's why it returned c as 99990. Thank you again.

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

추가 답변 (0개)

카테고리

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