2

** **

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.

Here is my code

public class Solution3 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        double mealCost = scan.nextDouble(); // original meal price
        int tipPercent = scan.nextInt(); // tip percentage
        int taxPercent = scan.nextInt(); // tax percentage
        double tip = mealCost *(tipPercent/100);
        double tax = mealCost*(taxPercent/100);
        double total= mealCost+tip+tax;
        // cast the result of the rounding operation to an int and save it as totalCost 
        int totalCost = (int) Math.round(total);

         System.out.println("The total meal cost is "+totalCost+" dollars");
    }
}

Expected Output

The total meal cost is 15 dollars.

My Output

The total meal cost is 12 dollars.

Please help me out.

Any help would be appreciated.

shmosel
  • 45,768
  • 6
  • 62
  • 130
Ketan G
  • 497
  • 1
  • 4
  • 20

2 Answers2

2

It's an integer division issue.

Try this:

double tip = mealCost *(tipPercent/100.0);
double tax = mealCost*(taxPercent/100.0);
codemirel
  • 170
  • 10
0

Calculate answer as:

double tip=(mealCost*tipPercent)/100;
double tax=(mealCost*taxPercent)/100;
Stephen Rauch
  • 44,696
  • 30
  • 102
  • 125