Search code examples
javaoperators

What changes can I make to pass this?


I am stuck on Day 2: Operator question on hackerrank. Here's the task:

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. Round the result to the nearest integer.

Here's my code:

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

class Result {
    public static void solve(double meal_cost, int tip_percent, int tax_percent) {

    // Write your code here
   {
   long total=0;double tip;double tax;

    

   tip = meal_cost*tip_percent/100;
   tax = tax_percent*tip_percent/100;
   total =Math.round(meal_cost+tip+tax);
   
   System.out.println(total);
   }
    }
}
//provided by hackerrank.
public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        double meal_cost = Double.parseDouble(bufferedReader.readLine().trim());

        int tip_percent = Integer.parseInt(bufferedReader.readLine().trim());

        int tax_percent = Integer.parseInt(bufferedReader.readLine().trim());

        Result.solve(meal_cost, tip_percent, tax_percent);

        bufferedReader.close();
    }
}

screenshot


Solution

  • In tax calculation, you are using tip_percent instead of meal_cost.

    Fix Result.solve by changing tax = meal_cost*tax_percent/100;

    Hope this helps !