Drill

ProblemsC++ › pricing

Split a gross amount into net and VAT

mediumpricingMathC++

Accounting receives invoice totals that already include VAT and needs the two halves separately.

vatSplit(gross: int, ratePercent: int) → Split

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

Split vatSplit(int gross, int ratePercent) {
    
}

Worked examples

CallResult
vatSplit(1200, 20)Split{1000, 200}
vatSplit(1000, 18)Split{847, 153}
vatSplit(1180, 18)Split{1000, 180}
vatSplit(999, 0)Split{999, 0}

Hint

net = (gross * 100 + half of the divisor) / (100 + rate), using integer division. Then subtract.

Reference solution in C++
Split vatSplit(int gross, int ratePercent) {
    int d = 100 + ratePercent;
    int net = (gross * 100 + d / 2) / d;
    return Split{net, gross - net};
}

The same problem in another language

More pricing problems in C++