1

** **

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
  • 49,289
  • 6
  • 73
  • 138
Ketan G
  • 507
  • 1
  • 5
  • 21

2 Answers2

1

It's an integer division issue.

Try this:

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

Calculate answer as:

double tip=(mealCost*tipPercent)/100;
double tax=(mealCost*taxPercent)/100;
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135