The for loop calculates the amount of money in a savings account after numberYears given an initial balance 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
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

Respuesta :

Answer:

  1. function savingsBalance = CalculateBalance(numberYears, savingsBalance)
  2. % numberYears: Number of years that interest is applied to savings
  3. % savingsBalance: Initial savings in dollars
  4. interestRate = 0.025;  
  5. % 2.5 percent annual interest rate
  6. % Complete the for loop to iterate from 1 to numberYears (inclusive)
  7.    for n = 1:numberYears
  8.        savingsBalance = savingsBalance + (savingsBalance * interestRate);
  9.    end
  10. end

Explanation:

The solution code is written in Matlab.

To complete the for loop, we just need to set the range of the years to enable the for loop to traverse through. We add expression 1:numberYears to enable the loop to iterate from 1 to numberYears. For example, if the input numberYears is 10, the for loop will run for 10 times by traversing the number from 1 to 10 (inclusive) and accumulating the savingsBalance for 10 years. The loop will stop after 10 rounds of iterations.

RELAXING NOICE
Relax