The for loop calculates the amount of money in a savings acc
The for loop calculates the amount of money in a savings account after numberYears given an initial balace of savingsBalance and an annual interest rate of 2.5%. Complete the for loop to iterate from 1 to numberYears (inclusive). function savingsBalance = CalculateBalance(numberYears, savingsBalance) numberYears: Number of years that interest is applied to savings savingsBalance: Initial savings in dollars 5 interestRate = 0.025; % 2.5 percent annual interest rate Complete the for loop to iterate from 1 to numberYears (inclusive) for () savingsBalance = savingsBalance + (savingsBalance * interestRate); end end Code to call your function when you click Run
Solution
function savingsBalance = CalculateBalance(numberYears, savingsBalance)
interestRate = 0.025;
for n = 1:numberYears
savingsBalance = savingsBalance + (savingsBalance*interestRate);
end
end
CalculateBalance(1,1000)
/*
Output
ans = 1025
Explanation
for n = 1:numberYears means it starts from 1 to numberYears(inclusive)
*/
