Clock, C++, CodeWars

Problem Statement

Clock shows h hours, m minutes and s seconds after midnight.

Your task is to write a function which returns the time since midnight in milliseconds.

Example:

h = 0
m = 1
s = 1

result = 61000

Input constraints:

0 <= h <= 23
0 <= m <= 59
0 <= s <= 59

Solving Process

Upon viewing the problem, we know that we need to convert the hours, minutes, and seconds into milliseconds. This means we need to have the number for how many milliseconds constitute an hour, minute, and second. This way, we can multiply the amount of [unit of time] by [number of milliseconds per that unit]. The conversions for the units are as follows: - Hour = 3,600,000 - Minute = 60,000 - Second = 1,000

Now that we know the units for conversion, and how to convert hours, minutes, and seconds, the next step is to get the total. In order to calculate the total, we must add the total amount of milliseconds together after each has been converted, and we will save this inside an int type variable Total.

int Total = h * 3600000 + m * 60000 + s * 1000;

After this, we just want to return the total of "Total", which we do like so.

return Total;

Solution

/********************************************
*
* Title: Clock
* Author: krisyotam
* Date: 2025-07-20
*
*
*********************************************/

using namespace std;

int past(int h, int m, int s) {
  int Total = h * 3600000 + m * 60000 + s * 1000;
  return Total;
}

Link To Solution on CodeWars

Bibliography

@misc{codewars-clock}
author Codewars
title Clock (Time to Milliseconds)
note C++ kata.
Last updated