Drill

ProblemsJava › finance

Price per unit

easyfinanceMathJava

A bulk bin lists a total for a quantity of identical items. What does a single unit cost?

pricePerUnit(totalMinor: int, quantity: 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 pricePerUnit(int totalMinor, int quantity) {
    
}

Worked examples

CallResult
pricePerUnit(1000, 4)250
pricePerUnit(1000, 3)333
pricePerUnit(100, 10)10
pricePerUnit(0, 5)0

Hint

Divide and drop the remainder.

Reference solution in Java
int pricePerUnit(int totalMinor, int quantity) {
    if (quantity <= 0) return 0;
    return totalMinor / quantity;
}

The same problem in another language

More finance problems in Java