Add hours on a clock (12-hour clock)

Challenge Level: Growing experience

Learning outcomes

Students will be able to:

Requirement:

Write a program that adds hours on a clock; for example, on a 12-hour clock, 11 o'clock plus 3 hours is 2 o'clock. This can be done using the modulo operator, except you need to adjust the output so that 0 in mod 12 is displayed as 12. See challenge "Add hours on a clock (24-hour clock)" if a 24-hour clock is usual in your country.

Testing examples:

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

Input Output
11
1
The new time is 12 o'clock.
11
4
The new time is 3 o'clock.
11
12
The new time is 11 o'clock.
11
72
The new time is 11 o'clock.

Languages

Scratch

What it should look like

Click on the green flag to see the expected output of your program.

Recommended blocks
when green flag clicked
set [original time v] to (answer)

set [hours to add v] to (answer)

set [new time v] to (((original time) + (hours to add)) mod (12))
ask [Enter a time:] and wait

ask [Enter the number of hours to add:] and wait
say [The new time is 12 o'clock.]

say (join [The new time is ] (join (new time) [ o'clock.]))
if <(new time) = [0]> then
else
end
Hints
  • Make variables called “original time” and “hours to add” and set their values to the inputs entered by the user. Make a variable called “new time” and set its value to the sum of the “original time” and “hours to add” modulo 12. Display the output as 12 o’clock, If the value of “new time” is equal to 0.

Show Scratch solution

Python