Drill

ProblemsJava › finance

Average monthly figures for a year

mediumfinanceArraysMathJava

Twelve monthly totals summarise a year. Report the average monthly figure, dropping the fractional minor unit.

yearlyAverage(monthlyTotals: list<int>) → int

Java 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

int yearlyAverage(List<Integer> monthlyTotals) {
    
}

Worked examples

CallResult
yearlyAverage(Main.<Integer>ls(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))1
yearlyAverage(Main.<Integer>ls(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12))6
yearlyAverage(Main.<Integer>ls(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))0
yearlyAverage(Main.<Integer>ls(100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100))100

Hint

Guard the length, sum the entries, divide by twelve.

Reference solution in Java
int yearlyAverage(List<Integer> monthlyTotals) {
    if (monthlyTotals.size() < 12) return 0;
    int sum = 0;
    for (int v : monthlyTotals) sum += v;
    return sum / 12;
}

The same problem in another language

More finance problems in Java