Drill

ProblemsC# › finance

Balance after yearly compounding

easyfinanceMathSimulationC#

An account reinvests its interest once a year. Report the balance after a whole number of years.

CompoundBalance(principal: int, ratePercent: int, years: int) → int

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public int CompoundBalance(int principal, int ratePercent, int years) {
    
}

Worked examples

CallResult
CompoundBalance(1000, 10, 2)1210
CompoundBalance(100, 5, 3)115
CompoundBalance(5000, 0, 5)5000
CompoundBalance(0, 10, 5)0

Hint

Loop the years, adding the floored interest each pass.

Reference solution in C#
public int CompoundBalance(int principal, int ratePercent, int years) {
    int balance = principal;
    for (int i = 0; i < years; i++) balance += (balance * ratePercent) / 100;
    return balance;
}

The same problem in another language

More finance problems in C#