Total cost of a subscription plan
A subscription charges a monthly rate for a given number of months. Annual plans get one month free.
- Base price equals monthlyAmount multiplied by months.
- If annual is true, subtract one month from the total.
- months of 0 or fewer returns 0.
SubscriptionTotal(monthlyAmount: int, months: int, annual: bool) → 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 SubscriptionTotal(int monthlyAmount, int months, bool annual) {
}
Worked examples
| Call | Result |
|---|---|
SubscriptionTotal(1000, 12, true) | 11000 |
SubscriptionTotal(1000, 12, false) | 12000 |
SubscriptionTotal(500, 6, true) | 2500 |
SubscriptionTotal(0, 12, false) | 0 |
Hint
Multiply first, then conditionally subtract.
Reference solution in C#
public int SubscriptionTotal(int monthlyAmount, int months, bool annual) {
if (months <= 0) return 0;
int total = monthlyAmount * months;
if (annual) total -= monthlyAmount;
return total;
}