Drill

ProblemsC# › finance

Months to reach a savings target

easyfinanceMathC#

A plan asks how many whole months of flat saving are needed to top up a target balance.

MonthsToTarget(target: int, monthlySaving: 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 MonthsToTarget(int target, int monthlySaving) {
    
}

Worked examples

CallResult
MonthsToTarget(1000, 300)4
MonthsToTarget(1000, 500)2
MonthsToTarget(1000, 1000)1
MonthsToTarget(0, 500)0

Hint

Ceil(target / saving) via (target + saving - 1) / saving.

Reference solution in C#
public int MonthsToTarget(int target, int monthlySaving) {
    if (target <= 0 || monthlySaving <= 0) return 0;
    return (target + monthlySaving - 1) / monthlySaving;
}

The same problem in another language

More finance problems in C#