Shops print each purchase on one line. Format one with printf.
What to build
A program that prints one line of a receipt.
Requirements
- Read an item name (one word), a quantity and a unit price.
- Print
ITEM x QTY @ PRICE = TOTALwhere PRICE and TOTAL have exactly two decimals.
Input
One line: item quantity price.
Output
One line.
Constraints
1 ≤ quantity ≤ 100; the price has at most two decimals and is below 1000.
Example
Input:
Notebook 3 2.50
Output:
Notebook x 3 @ 2.50 = 7.50
Why it works
printf with %.2f always shows two decimals, and the total is the quantity times the price.
Examples
Example 1 · Input
Notebook 3 2.50
Expected output
Notebook x 3 @ 2.50 = 7.50
Example 2 · Input
Lamp 1 19.99
Expected output
Lamp x 1 @ 19.99 = 19.99
Hints
Hint 1
fscanf(STDIN, "%s %d %f", $item, $qty, $price) reads the three values.
Hint 2
Use printf with %s, %d and %.2f, and do not forget the new line.
Solution
Show one correct solution (try it yourself first)
<?php
fscanf(STDIN, "%s %d %f", $item, $qty, $price);
printf("%s x %d @ %.2f = %.2f\n", $item, $qty, $price, $qty * $price);