Java Day 2: Operators

Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the meal's total cost.

Note: Be sure to use precise values for your calculations, or you may end up with an incorrectly rounded result!

  1. import java.util.Scanner;
  2. public class Solution {
  3. public static void main(String[] args) {
  4. Scanner scan = new Scanner(;
  5. double mealCost = scan.nextDouble(); // original meal price
  6. int tipPercent = scan.nextInt(); // tip percentage
  7. int taxPercent = scan.nextInt(); // tax percentage
  8. scan.close();
  9. // Write your calculation code here.
  10. double tip = mealCost * tipPercent / 100;
  11. double tax = mealCost * taxPercent / 100;
  12. // cast the result of the rounding operation to an int and save it as totalCost
  13. int totalCost = (int) Math.round(mealCost + tip + tax);
  14. // Print your result
  15. System.out.println(totalCost);
  16. }
  17. }
download android app

Popular HackerRank Java Solutions is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube