Skip to content
ExamSpine

Lesson 1.8 · Write it

Double it

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 whole number and prints it doubled, with a label, on one line:

Double: 14
  1. The line starts with Double: and one space.
  2. After the space comes the number multiplied by 2.
  3. Print nothing else. Spaces at the end are ignored.
  4. The number is a whole number, which can be 0 or negative.

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

In Python, use int to convert the typed line, compute the double, then join the label and the result with + (turn the number back into text with str). In Java, use Integer.parseInt. The multiplication happens before the joining even without brackets, but brackets around it make that easy to see.

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 number doubled with the label (rules 1 to 3)

    You type

    1. typed: 7

    It prints

    Double: 14
  • zero doubled is still zero (rules 2 and 4)

    You type

    1. typed: 0

    It prints

    Double: 0
solution.pySolution.java