Menu
Coddy logo textTech

Calculating Tip And Total

Part of the Fundamentals section of Coddy's Ruby journey — lesson 36 of 88.

challenge icon

Challenge

Easy

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

After displaying the confirmation message, calculate the following:

  1. The tip amount: multiply the bill by the tip percentage divided by 100
  2. The total bill: add the original bill and the tip amount

Store these in variables named tip_amount and total.

Then display the calculation results in the following format:

Tip amount: $[tip_amount]
Total with tip: $[total]

Use puts for all output and format numbers using %g to avoid unnecessary trailing zeros.

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

Welcome to the Bill Split Calculator!
Let's split your bill fairly.
Bill: $100
Tip: 20%
People: 4
Tip amount: $20
Total with tip: $120

Try it yourself

# TODO: Write your code below
# Use puts to display the welcome message
puts "Welcome to the Bill Split Calculator!\nLet's split your bill fairly."

# Read user inputs
bill = gets.chomp.to_f
tip_percent = gets.chomp.to_f
people = gets.chomp.to_f

# Display confirmation message
puts "Bill: $%g" % bill
puts "Tip: %g%%" % tip_percent
puts "People: %g" % people

All lessons in Fundamentals