Splitting The Bill
Part of the Fundamentals section of Coddy's Ruby journey — lesson 37 of 88.
Challenge
EasyIn the previous lesson, you calculated the tip amount and total bill. Now, add the final step: splitting the total among all the people.
After displaying the tip amount and total with tip, calculate how much each person should pay by dividing the total by the number of people.
Store this in a variable named per_person.
Then display the final result in the following format:
Each person pays: $[per_person]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
Each person pays: $30Try 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
# Calculate tip amount and total
tip_amount = bill * tip_percent / 100
total = bill + tip_amount
# Display calculation results
puts "Tip amount: $%g" % tip_amount
puts "Total with tip: $%g" % totalAll lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 47Bill Split Calculator
Welcome MessageGetting User Input2Variables and Data Types
Numbers and VariablesString Data TypeBoolean Data TypeSymbol Data TypeChecking Data TypesNaming ConventionsRecap - Variable Creation8Loops
For Loop with RangesWhile LoopBreakNextRecap - FactorialTimes LoopUntil LoopNested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators6Basic IO
Output with putsOutput with print and pOutput With VariablesInput with getsChomp MethodType ConversionRecap - Age CalculatorRecap - True or False