Menu
Coddy logo textTech

Calculating Tip And Total

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

challenge icon

Challenge

Easy

In the previous lesson, you gathered the bill total, tip percentage, and number of people from the user. Now, add the calculation logic to compute the tip amount and the total bill including the tip.

Using the values you already have, calculate:

  1. The tip amount: bill total multiplied by the tip percentage divided by 100
  2. The total with tip: bill total plus the tip amount

Remember to convert the tip percentage from Int to Double before performing the calculation.

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]

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

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) {
            print("Bill: \(billTotal)")
            print("Tip: \(tipPercentage)%")
            print("People: \(numberOfPeople)")
        }
    }
}

All lessons in Fundamentals