Check for odd/even numbers using repeated subtraction

Challenge Level: Growing experience

Learning outcomes

Students will be able to:

Requirement:

Create a program which asks the user to enter a number and checks if the number is odd or even. You should do this by keep subtracting 2 from your number until it is 0 or 1 (i.e. If it is 0 then the number is even and if it is 1 the number is odd). Make sure your program works for negative numbers as well.

Testing examples:

Your program should display the outputs shown in this table for the given inputs provided:

Input Output
265 You entered an odd number!
0 You entered an even number!
-22 You entered an even number!

Languages

Scratch

What it should look like

Click on the green flag, enter the inputs provided in the “testing examples” to see the expected output of your program.

Recommended blocks
when green flag clicked

ask [Enter a number:] and wait

change [number v] by (-2)
if <(number) < [0]> then
end

repeat until <<(number) = [0]> or <(number) = [1]>>
end

if <(number) = [0]> then
end

if <(number) = [1]> then
end
say [You entered an odd number!]

say [You entered an even number!]
set [number v] to [0]

set [number v] to (answer)

set [number v] to ((-1) * (number))
Hints
  • To check if a number is greater, less than or equal to another number use the <[] < []>, <[] > []>, or <[] = []>blocks under “Operators”.
  • You can make a negative number positive by multiplying it by -1.
  • The < <> or <> > operation under “Operators” joins two boolean blocks so any one of them can be true to return true. If at least one of them is true, the block returns true. If neither of them are true, it returns false.

Show Scratch solution