Drill

ProblemsGo › billing

Total cost of a subscription plan

mediumbillingMathGo

A subscription charges a monthly rate for a given number of months. Annual plans get one month free.

subscriptionTotal(monthlyAmount: int, months: int, annual: bool) → int

Go 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

func subscriptionTotal(monthlyAmount int, months int, annual bool) int {
	
}

Worked examples

CallResult
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 Go
func subscriptionTotal(monthlyAmount int, months int, annual bool) int {
	if months <= 0 {
		return 0
	}
	total := monthlyAmount * months
	if annual {
		total -= monthlyAmount
	}
	return total
}

The same problem in another language

More billing problems in Go