Drill

ProblemsC++ › billing

Compute the final charge on an order

easybillingMathC++

Combine a subtotal, an optional discount, and shipping to produce the final amount due.

finalCharge(subtotal: int, discount: int, shipping: 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

int finalCharge(int subtotal, int discount, int shipping) {
    
}

Worked examples

CallResult
finalCharge(5000, 500, 800)5300
finalCharge(1000, 2000, 500)500
finalCharge(3000, 0, 1000)4000
finalCharge(0, 0, 0)0

Hint

Max(0, subtotal − discount) then add shipping.

Reference solution in C++
int finalCharge(int subtotal, int discount, int shipping) {
    int net = subtotal > discount ? subtotal - discount : 0;
    return net + shipping;
}

The same problem in another language

More billing problems in C++