# Day 2: Operators

# Day 2: Operators

+ 0 comments This is the cleanest code I could come up with:

double tip = (double) tip_percent/100 * meal_cost; double tax = (double) tax_percent/100 * meal_cost; long total_cost = Math.round(meal_cost + tip + tax); System.out.println(total_cost);

The main complication here is getting rid of the integer division that will return zero for tip_percent/100. tip_percent is an integer. You can force a real division by having any element of the expression be a double, such as writing tip_percent/100.00 or by explicitly casting to double, which I think is more straightforward. Math.round takes a double and returns a long, so total_cost is a long. You could cast it to an int, but there is no need.

It can be all written in single expression, though not as readable:

System.out.println(Math.round(meal_cost * (1 + (double) tip_percent / 100 + (double) tax_percent / 100)));

+ 0 comments import java.io.

*; import java.math.*; import java.security.*; //import java.atext.*; 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) { double tip, tax, total_cost; tip = meal_cost * tip_percent/100; tax = tax_percent * meal_cost/100 ; total_cost = meal_cost + tip + tax; int round = (int) Math.round(total_cost); System.out.println(round); }`

}

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(); }`

}

+ 0 comments In js this is the solution

`function solve(meal_cost, tip_percent, tax_percent){ var tipValue = meal_cost / 100 * tip_percent; var taxValue = tax_percent / 100 * meal_cost; var totalCost = meal_cost + tipValue + taxValue; console.log(Math.round(totalCost)) }`

+ 0 comments """ Python3 Solution-paseed all test case"""

#!/bin/python3 import math import os import random import re import sys # # Complete the 'solve' function below. # # The function accepts following parameters: # 1. DOUBLE meal_cost # 2. INTEGER tip_percent # 3. INTEGER tax_percent # # Write your code here def solve(): meal_cost=float(input().strip()) tip_percent=int(input().strip()) tax_percent=int(input().strip()) percetage_cal=lambda x,y: (x*y)/100 total_cost=round(meal_cost+percetage_cal(meal_cost,tip_percent)+percetage_cal(meal_cost,tax_percent)) return total_cost print(solve())

+ 1 comment Resultado esta dando 14 (?) class Result {

`double meal_cost = 12.0; int tip_percent = 20; int tax_percent = 8; public static void solve(double meal_cost, int tip_percent, int tax_percent) { double gorjeta = (meal_cost/100)*tip_percent; double taxa =(tax_percent/100)*meal_cost; double total_custo = meal_cost + gorjeta + taxa; int round = (int) Math.round(total_custo); System.out.println(round); }`

}

Sort 3273 Discussions, By:

Please Login in order to post a comment