Start learning free

LESSON 21 OF 347 · CALCULATIONS WITH NUMBERS

Convert Hours and Minutes to Seconds in C#: Unit Conversion

Lesson Read the full explanation

We created two variables to store the number of hours and minutes we worked on a project. We want to convert this amount of time into seconds.

Use these two variables and some arithmetic operations to convert and print to the console the number of seconds the hours and minutes add up to.


Example:

For example, if hours was 1 and minutes was 2, the answer would be 3720.

Why? Because one hour and two minutes add up to 3720 seconds.


Need a hint?


1 hour is made up of 60 minutes. To how many minutes do hours and minutes add up?

hours and minutes add up to hours * 60 + minutes minutes.

Now, how many seconds are there in hours * 60 + minutes minutes?



As 1 minute is made up of 60 seconds, and we have (hours * 60 + minutes) minutes, the answer is: (hours * 60 + minutes) * 60


STEP 1 OF 1
    Ctrl + Enter to check

    C# · CALCULATIONS WITH NUMBERS

    Convert Hours And Mins Into Seconds in C#, explained

    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.

    1. Hours to minutes. Multiply by 60: hours * 60 is 780.
    2. Add the extra minutes. hours * 60 + minutes is 780 + 47 = 827 minutes in total.
    3. Minutes to seconds. Multiply the total by 60: 827 * 60 = 49620.
    4. 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 49620 passes 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.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free