Part 02 / Topic 8 of 8
Putting it together: the restaurant bill splitter
Sections 1–7 together, in one program: val/var, inferred types, string templates, arithmetic
operators and readln() input, reading a food subtotal, a diner count and a tip percent, then
working out GST, tip and an equal per-head share.
fun main() {
val restaurant = "Saffron Kitchen, Koramangala, Bengaluru"
val gstPercent = 5.0 // a non-AC restaurant's GST slab; fixed for this version
println("===========================================")
println(" $restaurant")
println("===========================================")
println("Enter the food subtotal in rupees:")
val subtotal = readln().trim().toDouble()
println("Enter the number of diners:")
val diners = readln().trim().toInt()
println("Enter the tip percent you want to leave:")
val tipPercent = readln().trim().toDouble()
val gstAmount = subtotal * gstPercent / 100
val tipAmount = subtotal * tipPercent / 100
val total = subtotal + gstAmount + tipAmount
val perHead = total / diners
println("-------------------------------------------")
println("Subtotal : ₹$subtotal")
println("GST ($gstPercent%) : ₹$gstAmount")
println("Tip ($tipPercent%) : ₹$tipAmount")
println("Total : ₹$total")
println("Diners : $diners")
println("-------------------------------------------")
println("Each person pays: ₹$perHead")
}
$ kotlinc examples/06_bill_splitter.kt -include-runtime -d bill_splitter.jar
$ printf '2400\n4\n10\n' | java -jar bill_splitter.jar
===========================================
Saffron Kitchen, Koramangala, Bengaluru
===========================================
Enter the food subtotal in rupees:
Enter the number of diners:
Enter the tip percent you want to leave:
-------------------------------------------
Subtotal : ₹2400.0
GST (5.0%) : ₹120.0
Tip (10.0%) : ₹240.0
Total : ₹2760.0
Diners : 4
-------------------------------------------
Each person pays: ₹690.0
gstAmount and tipAmount are both subtotal * percent / 100 — multiplication before division
here, deliberately, so the intermediate value (subtotal * percent) stays a whole number of
rupee-percent units before the division; swapping the order (subtotal / 100 * percent) gives the
same final answer for these inputs, but only because 100 divides them cleanly — it's * then
/ as the habit to keep once the numbers stop being this tidy. Every value in the receipt is a
val: this program runs top to bottom once, so nothing here ever needed reassigning. Part 3 adds
a profile lookup that can genuinely come back empty, which is where var and null-safety together
start to matter.
Full program: examples/06_bill_splitter.kt.
Done here? The practice set for Part 2 — including the solutions — is on the part page.