Menu
Coddy logo textTech

Splitting The Bill

Part of the Fundamentals section of Coddy's Swift journey — lesson 40 of 86.

challenge icon

Challenge

Easy

In the previous lesson, you calculated the tip amount and total with tip. Now, add the final calculation to determine how much each person pays when splitting the bill.

Using the totalWithTip you already calculated and the numberOfPeople, calculate the amount each person should pay by dividing the total by the number of people.

Remember to convert numberOfPeople from Int to Double before performing the division.

Update your output to print in this exact format:

Welcome to the Bill Split Calculator!
Bill: [billTotal]
Tip: [tipPercentage]%
People: [numberOfPeople]
Tip amount: [tipAmount]
Total with tip: [totalWithTip]
Each person pays: [amountPerPerson]

For example, if the inputs are 100.0, 20, and 4, the output should be:

Welcome to the Bill Split Calculator!
Bill: 100.0
Tip: 20%
People: 4
Tip amount: 20.0
Total with tip: 120.0
Each person pays: 30.0

Try it yourself

print("Welcome to the Bill Split Calculator!")

if let billInput = readLine(), let billTotal = Double(billInput) {
    if let tipInput = readLine(), let tipPercentage = Int(tipInput) {
        if let peopleInput = readLine(), let numberOfPeople = Int(peopleInput) {
            let tipAmount = billTotal * Double(tipPercentage) / 100
            let totalWithTip = billTotal + tipAmount
            
            print("Bill: \(billTotal)")
            print("Tip: \(tipPercentage)%")
            print("People: \(numberOfPeople)")
            print("Tip amount: \(tipAmount)")
            print("Total with tip: \(totalWithTip)")
        }
    }
}

All lessons in Fundamentals