easyProgramming Fundamentals
Data Types & Variables
Read a whole-number quantity and a decimal unit price. Calculate the total price.
Work through the cases
3 examplesInput notation
Quantity and price on one line. Both are nonnegative.Expected result
The total with exactly two decimal places.Case 1
Input
3 2.50
Expected output
7.50
Case 2
Input
2 0.75
Expected output
1.50
Case 3
Input
0 99.99
Expected output
0.00
These notes use simplified input/output notation. The linked platform defines the method signature and judge format.
FROM IDEA TO IMPLEMENTATION
Understand the approach.
Compare the documented approaches below. Brute force, better, and optimal versions appear where available; a direct exercise may need only one approach.
Multiply the quantity by the unit price and format two decimal places. Time and space: O(1).
import java.util.*;
public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in).useLocale(Locale.US); System.out.printf(Locale.US, "%.2f%n", in.nextInt() * in.nextDouble()); } }Reference code. Platform solutions may use supplied node types, imports, or collection libraries; foundational examples are complete programs.