HackerRank.com 30 Days of Code – Day 2: Operators

Day 2 of HackerRank’s 30 days of code was a fairly simple task designed to teach operators.

import java.util.*;
import java.math.*;

public class Arithmetic {

    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 totalCostOfMeal = 0;
        double tipCost = 0;
        scan.close();
      
        // Write your calculation code here.
        totalCostOfMeal = (mealCost + (mealCost * (tipPercent/100))) * ((taxPercent/100) + 1);
        //System.out.println("mealCost: " + mealCost);
        //System.out.println("tipPercent: " + (((float)tipPercent/100) + 1));
        //System.out.println("taxPercent: " + (((float)taxPercent/100) + 1));
        tipCost = (((float)tipPercent/100) * mealCost);
        //System.out.println("tipCost: " + tipCost);
        totalCostOfMeal = (mealCost*(((float)taxPercent/100) + 1)) + tipCost;
        //System.out.println("totalCostOfMeal: " + totalCostOfMeal);
        
      
        
        // cast the result of the rounding operation to an int and save it as totalCost 
        int totalCost = (int) Math.round(totalCostOfMeal);
      
        // Print your result
        System.out.println("The total meal cost is " + totalCost + " dollars.");
    }
}

Comments are closed.