Objective
In this challenge, you'll work with arithmetic operators. Check out the Tutorial tab for learning materials and an instructional video!
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.
Note: Be sure to use precise values for your calculations, or you may end up with an incorrectly rounded result!
Input Format
There are 3 lines of numeric input:
The first line has a double, mealCost(the cost of the meal before tax and tip).
The second line has an integer, tipPercent(the percentage of mealCost being added as tip).
The third line has an integer, taxPercent(the percentage of mealCost being added as tax).
Output Format
Print The total meal cost is totalCost dollars., where totalCost is the rounded integer result of the entire bill ( mealCost with added tax and tip).
Sample Input
12.00
20
8
Sample Output
The total meal cost is 15 dollars.
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
scan.close();
// Write your calculation code here.
mealCost = mealCost + (mealCost*(tipPercent*0.01)) + (mealCost*(taxPercent*0.01));
// cast the result of the rounding operation to an int and save it as totalCost
int totalCost = (int) Math.round(mealCost);
// Print your result
System.out.println("The total meal cost is "+totalCost+" dollars.");
}
}
'IT > Programming' 카테고리의 다른 글
[HackerRank] Day 4: Class vs. Instance (0) | 2023.04.17 |
---|---|
[HackerRank] Day 3: Intro to Conditional Statements (0) | 2023.04.17 |
[HackerRank] Day 1: Data Types (0) | 2023.04.17 |
[HackerRank] Day 0: Hello, World. (0) | 2023.04.17 |
[백준, Python] 7567번 그릇 (0) | 2022.08.17 |