Calculating compound interest using a for loop

-Someone saves a sum of 100 Euro on the bank against an interest rate of 5%. The interest is paid at the end of each year. Calculate the total sum after 16 years using a for-loop.
-Calculate the total sum when in the 7th year the interest rate is increased to 6%. To do this, insert an if-statement inside the for-next loop.

5 个评论

Sounds a bit like a homework question. At the very least it might be worth showing that you've had a crack at writing something to solve the problem, even if it doesn't work.
It's not homework I'm trying to teach myself how to code
%Someone saves a sum of 100 Euro on the bank against an
%interest rate of 5%. The interest is paid at the end of
%each year. Calculate the total sum after 16 years
%using a for-loop
a=zeros(1,16);
a(1)=100
for i=2:16
a(i)=a(i-1)+.......... %define the formula
end
%a(16) in command window
What about:
savings=100;
for i=1:16
savings=savings*1.05
end
After all, you don't need to keep a record of the savings after every year
this actually does give the savings after every year, I think it works! is it possible to store this in a matrix and then supress the matrix with ; and just give the result after 16 years using something like (1,16)
You could store it in a matrix if you felt the need (though the question posed originally does not require the value to be stored, only the result after 16 years). You've already done something similar in the attempt you made, so think about how would you adapt it to meet your requirements?

请先登录,再进行评论。

类别

帮助中心File 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