Skip to content
ExamSpine

Lesson 1.8 · Write it

Total price

A whole programPython or Java5 tests, 3 of them hidden

On a phone? Put the lines in order instead

Write a program that reads a price (a decimal number) on the first line and a quantity (a whole number) on the second line, and prints the total on one line.

  1. The total is the price multiplied by the quantity.
  2. Print only the total, as a number. Spaces at the end are ignored.

Your program is run several times. Only what it prints is checked.

In Python, use float for the price and int for the quantity. In Java, use Double.parseDouble for the price and Integer.parseInt for the quantity.

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 5 times: the 2 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • a decimal price times a quantity (rule 1)

    You type

    1. typed: 2.50
    2. typed: 3

    It prints

    7.5
  • zero items (rules 1 and 2)

    You type

    1. typed: 5.00
    2. typed: 0

    It prints

    0.0
solution.pySolution.java