Lesson 4.3 · Write it
Double a number
A functionPython or Java6 tests, 3 of them hidden
On a phone? Put the lines in order instead
Write a function that returns twice the value of its input.
nis a whole number from -1000 to 1000.- Return
nmultiplied by 2.
Only the number your function returns is checked.
def double_value(n: int) -> int
Examples
Your function is tested on 6 cases: the 3 examples below, and 3 hidden ones that check the rules above.
- double_value(3) should return 6: a small positive number (rule 2)
- double_value(0) should return 0: zero doubled is zero (rule 2)
- double_value(-4) should return -8: a negative number (rule 2)
public static int doubleValue(int n)
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.Checking whether Java answers can run here.
Examples
Your function is tested on 6 cases: the 3 examples below, and 3 hidden ones that check the rules above.
- doubleValue(3) should return 6: a small positive number (rule 2)
- doubleValue(0) should return 0: zero doubled is zero (rule 2)
- doubleValue(-4) should return -8: a negative number (rule 2)