Calculating Tip And Total
Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 43 of 93.
Challenge
EasyIn the previous lesson, you collected the bill amount, tip percentage, and number of people. Now, add the tip calculation logic to compute the tip amount and total bill.
Using the variables you already have, calculate:
tipAmount: the tip based on the bill amount and tip percentagetotalBill: the sum of the original bill and the tip amount
To calculate the tip amount, multiply the bill amount by the tip percentage divided by 100.
Update your output to display the tip amount and total bill instead of the raw inputs:
Welcome to Bill Split Calculator!
Tip amount: [tipAmount]
Total bill: [totalBill]For example, if the inputs are 100.0, 20, and 4, the output should be:
Welcome to Bill Split Calculator!
Tip amount: 20.0
Total bill: 120.0Input constraints: the bill and tip percentage are nonnegative, and the number of people is a positive integer. Numeric inputs are valid and finite.
Try it yourself
fun main() {
// TODO: Write your code below to print the welcome message
println("Welcome to Bill Split Calculator!")
val billAmount = readLine()!!.toDouble()
val tipPercentage = readLine()!!.toInt()
val numberOfPeople = readLine()!!.toInt()
println("Bill amount: $billAmount")
println("Tip percentage: $tipPercentage")
println("Number of people: $numberOfPeople")
}All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorAugmented AssignmentComparison OperatorsRecap - Simple Math7Basic IO
Println FunctionString TemplatesReadLine InputType ConversionRecap - Years Until RetirementRecap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesNamed ArgumentsDefault ValuesSingle Expression FunctionsRecap - Sigma FunctionRecap - Validation Function2Variables
Val vs VarType InferenceNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Logical Operators Part 3Ternary With If ExpressionRecap - Simple Logic8Bill Split Calculator
Welcome MessageGetting Input3Nullability
What Is Null SafetyNullable TypesSafe Call OperatorElvis OperatorFunction Challenge BasicsNot Null AssertionRecap - Safe Access6Decision Making
If StatementIf - ElseIf As An ExpressionWhen ExpressionWhen With RangesRecap - Simple Calculator9Loops
For LoopWhile LoopDo-While LoopBreakContinueRanges In LoopsNested LoopRecap - FactorialRecap - Dynamic InputPractice on your own: Kotlin playground