Apply a promo percentage
The till applies a promo code to a price held in minor units — kuruş, cents, whatever the currency splits into.
- A percentage outside 1 to 100 is a bad code: return the price untouched.
- Stay in integers and drop the remainder, so 10% off 999 is 900, not 899.1.
DiscountedPrice(price: int, percent: 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.
Where you start
public int DiscountedPrice(int price, int percent) {
}
Worked examples
| Call | Result |
|---|---|
DiscountedPrice(1000, 20) | 800 |
DiscountedPrice(999, 10) | 900 |
DiscountedPrice(1000, 0) | 1000 |
DiscountedPrice(1000, 150) | 1000 |
Hint
Reject the bad range first, then integer-divide.
Reference solution in C#
public int DiscountedPrice(int price, int percent) {
if (percent < 1 || percent > 100) return price;
return price - (price * percent) / 100;
}