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(System.in);
  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. }
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]


donate a cup of tea :)


Join Our Facebook Group

Share this solution


download android app

Popular HackerRank Java Solutions








codesadda.com

Codesadda.com 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