What the exercise asks
The editor has two int variables, hours (13) and minutes (47), describing how long you worked on a project. You print the same amount of time in seconds. For the statement's example, 1 hour and 2 minutes, the answer is 3720. For the starting values it is 49620.
Converting in two stages
Seconds are not a multiple of hours directly in your head, so go through minutes, which sit in the middle. There are 60 minutes in an hour and 60 seconds in a minute.
- Hours to minutes. Multiply by 60:
hours * 60is780. - Add the extra minutes.
hours * 60 + minutesis780 + 47 = 827minutes in total. - Minutes to seconds. Multiply the total by 60:
827 * 60 = 49620. - Print the result with
Console.WriteLine.
Another route is to convert each part on its own, hours * 3600 + minutes * 60. It gives the same answer, because an hour is 3600 seconds. The lesson's version builds the total minutes first.
Why the parentheses are needed
The final multiplication must apply to the whole sum of minutes. Without parentheses, multiplication runs before addition, so hours * 60 + minutes * 60 would turn only the minutes into seconds and leave the hours as minutes. For 13 hours and 47 minutes that prints 3600, which is far too small. Wrapping (hours * 60 + minutes) makes the addition happen first, so one * 60 converts everything.
The solution in C#
int hours = 13;
int minutes = 47;
Console.WriteLine((hours * 60 + minutes) * 60);
The inner hours * 60 runs first, then the addition inside the parentheses, then the outer multiplication.
Integers are the right type here
Hours and minutes are whole numbers, and converting between them needs only multiplication, so int is a natural fit and no rounding happens. An int holds values up to about 2.1 billion. As seconds, that is roughly 68 years, so a very long duration could exceed it. In that case use long. When an int calculation goes past its limit, C# does not raise an error by default and the result wraps around to a wrong value.
Naming the numbers
The two 60s in the expression are "magic numbers": a reader has to know what each one means. For larger programs, give them names with const:
const int SecondsPerMinute = 60;
const int MinutesPerHour = 60;
int total = (hours * MinutesPerHour + minutes) * SecondsPerMinute;
Console.WriteLine(total); // 49620
A const must be given its value when it is declared and can never change afterwards. The exercise itself is small enough that plain 60 is fine.
Going the other way
The reverse conversion, from seconds back to hours, minutes and seconds, uses integer division and the remainder operator, which the next lessons in this chapter introduce. For example, 3725 / 3600 is the whole number of hours, 1, and 3725 % 3600 is what is left over, 125 seconds.
Common mistakes
- Missing parentheses, as described above.
- Using 100 or 24 by habit. Time units use 60, not 100.
- Converting only one part. If you print just
hours * 3600, the minutes are ignored. - Hard-coding the answer. Printing
49620passes the starting values but fails when the checker supplies others. Always compute from the variables.
Testing
Check your formula on inputs you can verify without a calculator. With hours = 1 and minutes = 0 the output should be 3600. With hours = 0 and minutes = 1 it should be 60. With hours = 1 and minutes = 2 it should be 3720.
If a result is wrong, work the same input by hand and compare each stage with what your code produces. Printing the minutes total on its own line while you experiment, such as Console.WriteLine(hours * 60 + minutes);, shows which stage went wrong. Remove that extra print before you submit, because the checker expects a single number.
Key takeaways
- Convert in stages: hours to minutes, add minutes, then minutes to seconds.
- Parentheses make an addition run before a multiplication.
- Always compute from the variables rather than typing the final number.