Simple Cashier Program Source Code in Java
Now we are already in Day 2 in 30 day code challenge from hackerrank. This task will test you some uses of operators. As you know mathematic operations have to use several operator like “+” for sum, “-” for subtraction, “*” for multiplication, “/” for division, “%” for modulo operation, “( )” for do an operation which inside of it first, and so on.
In here, you will make a program like a cashier. Given a meal cost, tip percentage, and tax percentage will be resulted a total amount of money customer must be paid. Image below show you how the program should be.
After understanding the algorithm of program, then here the solution :
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. double tip,tax, total; tip = mealCost * ((double)(tipPercent/100.0)); tax = mealCost * ((double)(taxPercent/100.0)); 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); // Print your result System.out.println("The total meal cost is "+totalCost+" dollars." ); } }
The result of listing program above after run is like this.
And here another input test :
Comments
Post a Comment