How to create a while loop that can determine how many years do you have to invest $100000 at a compound interest of 5% per year to double your money?

4 views (last 30 days)
I need help basically doing the same thing in the picture I have attached but using a while loop.

Accepted Answer

KSSV
KSSV on 24 Mar 2017
Amount = 100000 ;
Amount0 = Amount ;
yr = 0 ;
while Amount <= 2*Amount0
yr = yr+1 ;
Amount = Amount*1.05 ;
fprintf('Amount at year %d is %.2f\n',yr,Amount)
end

More Answers (2)

Image Analyst
Image Analyst on 24 Mar 2017
Edited: Image Analyst on 24 Mar 2017
Try this
Amount = 100000
numYears = 0;
while Amount < 200000
Amount = Amount * 1.05
numYears = numYears + 1;
fprintf('After %d years, the Amount = %.2f\n', numYears, Amount);
end

Roger Stafford
Roger Stafford on 24 Mar 2017
Who needs a loop to do this trivial problem? We want to solve for n in the equation
100000*(1.05)^n = 200000
Rewrite it as
n*log(1.05) = log(2)
n = log(2)/log(1.05)
Matlab version:
n = ceil(log(2)/log(1.05));
  2 Comments
Steven Lord
Steven Lord on 24 Mar 2017
Who needs a loop to do this trivial problem?
The student whose professor assigned them this homework to teach them how to use while loops and insists they use a while loop to solve it. :)
Roger Stafford
Roger Stafford on 24 Mar 2017
Yes, Steven, you are right. However, I wish those same professors would choose problems using while loops that were not so easy to do in one’s head. That way the student might be more motivated to making good use of a while loop where it was apparent that it would be difficult to do without it.

Sign in to comment.

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!