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

public 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#
public 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#