Add hours on a clock (24 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 24-hour clock, 11 o'clock plus 3 hours is 14:00. This can be done using the modulo operator.

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:00.
11
4
The new time is 15:00.
11
12
The new time is 23:00.
22
2
The new time is: 00:00
22
4
The new time is: 2:00
22
22
The new time is: 20:00

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 (24))
ask [Enter a time:] and wait

ask [Enter the number of hours to add:] and wait
say [The new time is 00:00]

say (join [The new time is ] (join (new time) [:00.]))
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 24. Display the output as 00:00, If the value of “new time” equal to 0.

Show Scratch solution

Python

Extra Challenge

Modify your program so the new time is displayed as 24:00, if the value of the “new time” is equal to 0.