How savings in a bank account grows with interest.
★☆☆Compound interest, also known as compounding, is when you earn money (interest) on both the money you save and the interest you earn on that saving. So every year when you earn interest, you also earn interest on the interest itself.
Write a program that outputs the balance each year from three inputs: the balance in pounds sterling, the percentage interest rate and the target balance to achieve. The program stops when the target balance is reached.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
compound that:Hint
The statement
interest_rate = interest_rate / 100;will convert the interest rate into a number you can multiply by the balance to calculate the new balance.
Enter the balance to the nearest pound: £
1000
Enter the % interest rate:
3
Enter the target balance to the nearest pound: £
1300
Year: 1: Balance: £1030
Year: 2: Balance: £1060
Year: 3: Balance: £1091
Year: 4: Balance: £1123
Year: 5: Balance: £1156
Year: 6: Balance: £1190
Year: 7: Balance: £1225
Year: 8: Balance: £1261
Year: 9: Balance: £1298
Year: 10: Balance: £1336
// Compound interest program
using System;
class Submission
{
---
// -------------------------
// Subprograms
// -------------------------
// Calculate compound interest
---
static void compound(int balance, double interest_rate, int target_balance)
{
---
int year = 1;
interest_rate = interest_rate / 100;
---
// Calculate interest until the balance matches the target balance
while (balance < target_balance)
{
---
double interest = balance * interest_rate;
balance = (int)(balance + interest);
Console.WriteLine($"Year: {year}: Balance: £{balance}");
year = year + 1;
---
}
---
}
---
// -------------------------
// Main program
// -------------------------
public static void Main(string[] args)
{
---
Console.WriteLine("Enter the balance to the nearest pound: £");
int balance = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the % interest rate:");
double interest_rate = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter the target balance to the nearest pound: £");
int target_balance = Convert.ToInt32(Console.ReadLine());
---
compound(balance, interest_rate, target_balance);
---
}
---
}