
Day 2 of 30 Days of Code is a restaurant bill problem. You know the price of a meal, the tip percentage and the tax percentage, and you must print the final amount rounded to a whole number. The maths is school level. What the problem actually tests is whether you understand how Java treats numbers: the difference between int and double, what happens when you divide two integers, and how rounding works.
These are not small details. A large share of "my answer is off by one" bugs in contests and in real billing code come from integer division or careless rounding. Getting comfortable with them on an easy problem saves you a lot of confusion later.
Problem
A meal has a base price. A tip is added as some percentage of that base price, and tax is added as another percentage of the same base price (tax is not charged on the tip). Find the total and print it rounded to the nearest integer.
Input format: three lines. Line 1 is the meal cost, a decimal number. Line 2 is the tip percent, an integer. Line 3 is the tax percent, an integer.
Output format: a single integer, the total cost rounded to the nearest whole number.
Constraints: the old statement does not list explicit limits. The values are small enough that double holds them comfortably.
Input:
12.00
20
8
Output:
15
Approach
- Read the meal cost as a
double, because it can have a fractional part like 12.50. - Read the two percentages as
int. - Tip = mealCost × tipPercent / 100. Tax = mealCost × taxPercent / 100. Both are based on the original meal cost.
- Total = mealCost + tip + tax.
- Round the total to the nearest integer and print it.
Dry run on the sample: mealCost = 12.0, tip = 12.0 × 20 / 100 = 2.4, tax = 12.0 × 8 / 100 = 0.96. The total is 12.0 + 2.4 + 0.96 = 15.36. Rounding 15.36 to the nearest integer gives 15, which matches the expected output.
One thing to think about before coding: what if you computed tipPercent / 100 first? With two integers, 20 / 100 is 0 in Java, because integer division throws away the fraction. Your tip would silently become zero. You avoid this either by multiplying by the double first, or by dividing by 100.0 instead of 100. The solution below does both, so the order of operations cannot hurt you.
Solution in Java
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double mealCost = sc.nextDouble();
int tipPercent = sc.nextInt();
int taxPercent = sc.nextInt();
sc.close();
// Divide by 100.0 (a double) so we never fall into integer division
double tip = mealCost * tipPercent / 100.0;
double tax = mealCost * taxPercent / 100.0;
double total = mealCost + tip + tax;
// Math.round(double) returns a long, rounding .5 upwards
long totalCost = Math.round(total);
System.out.println(totalCost);
}
}
The Java pieces used here, in plain words:
sc.nextDouble()reads the next number and allows a decimal point. It also accepts input like12without a decimal part.sc.nextInt()reads the next whole number. Scanner skips the newlines between lines on its own, so three separate lines are no problem.Math.round(double)rounds to the nearest whole number and returns along. Halves go up, so 12.5 becomes 13. This is different from casting with(int) total, which simply chops off the decimal part (15.9 would become 15).
The older solution on this page divided meal_cost / 100 first, which is safe because meal_cost is a double. It worked. The version above just makes the safety obvious to anyone reading it.
I ran it on the sample and on three extra cases. 10.25, 17, 5 gives a raw total of 12.505 and prints 13. 15.50, 15, 10 gives 19.375 and prints 19. 10, 0, 0 prints 10, showing that zero tip and zero tax work fine.
Output
15
Complexity
O(1) time and O(1) space. There is a fixed amount of arithmetic, no matter what the input values are.
Common mistakes
- Integer division: writing
tipPercent / 100 * mealCost. With integers,tipPercent / 100is 0 for any tip under 100 percent, so the tip vanishes. - Casting instead of rounding:
(int) totaltruncates. For a total of 15.6 it prints 15 instead of 16. - Calculating tax on (meal + tip). The problem says both percentages apply to the base meal cost only.
- Printing the
doubledirectly, which gives15.0or15.36instead of the integer the judge expects.
Previous: Day 1: Data Types. Next: Day 3: Intro to Conditional Statements.
Tip: try to solve the problem yourself before reading the solution. Use the code as a reference to check your approach, and make sure you understand why it works.
More HackerRank solutions All coding problems MCQ practice
See everything in one place: Coding Problems and Solutions
Comments
Post a Comment