Start learning free

LESSON 22 OF 348 · CALCULATIONS WITH NUMBERS

Convert Hours and Minutes into Seconds in Kotlin

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

    KOTLIN · CALCULATIONS WITH NUMBERS

    Convert Hours And Mins Into Seconds in Kotlin, explained

    Problem overview

    Two variables record how long you worked on a project: hours, which is 13, and minutes, which is 47. The task is to print the total time in seconds. The lesson's example is 1 hour and 2 minutes, which is 3720 seconds.

    Approach

    Seconds are the smallest unit here, so everything has to be converted down to them. The usual route goes through minutes:

    • An hour has 60 minutes, so hours * 60 is the number of minutes in the hours.
    • Adding minutes gives the total in minutes: hours * 60 + minutes.
    • A minute has 60 seconds, so multiplying that total by 60 gives seconds.

    Checking with the example: 1 hour is 60 minutes, plus 2 is 62 minutes, and 62 times 60 is 3720.

    Step by step

    1. Turn the hours into minutes. hours * 60 is 780.
    2. Add the minutes. hours * 60 + minutes is 827. Multiplication runs first, so no parentheses are needed yet.
    3. Convert the total to seconds. Multiply by 60 again. Here the parentheses are essential: the sum must be computed before the final multiplication.
    4. Print the result. println takes the whole expression.

    The solution in Kotlin

    val hours: Int = 13
    val minutes: Int = 47
    
    println((hours * 60 + minutes) * 60)
    

    The program prints 49620, which is 827 minutes times 60. Details worth knowing:

    • The parentheses change the answer. Without them, hours * 60 + minutes * 60 would multiply only the minutes by 60 a second time. It converts the hours to minutes but the minutes to seconds, then adds the two, and prints 3600. Both parts must be in the same unit before they are added.
    • Another correct form. hours * 3600 + minutes * 60 converts each part to seconds separately and then adds them. For these inputs it also prints 49620.
    • The result fits in an Int. An Int holds values up to 2147483647, which is about 596,000 hours in seconds. A realistic number of hours never gets near that.

    Thinking in units

    Unit conversion is mostly bookkeeping. Each factor of 60 moves a quantity one unit down: hours to minutes, minutes to seconds. Multiplying makes the number bigger because the same time is counted in a smaller unit. A good habit is to name the unit of every intermediate value. After hours * 60 you hold minutes, after adding minutes you still hold minutes, and only the last multiplication produces seconds.

    The same pattern works for other conversions, such as days to hours (a factor of 24) or kilometres to metres (1000). To go the other way, from seconds back to hours and minutes, integer division and the remainder operator do the work: dividing by 3600 gives whole hours, and the remainder of that division, divided by 60, gives the minutes.

    Edge cases and common mistakes

    • Mixing units. hours * 60 + minutes * 60 adds minutes to seconds and prints 3600 for these values, not 49620.
    • Stopping at minutes. hours * 60 + minutes is 827, the total in minutes. The task asks for seconds, so the final factor of 60 is needed.
    • Using 24 or 100. A minute and an hour both convert by 60, not by 100.
    • Zero values. With hours = 0 and minutes = 0 the answer is 0, as it should be.

    Testing

    • hours = 1, minutes = 2 prints 3720.
    • hours = 0, minutes = 1 prints 60.
    • hours = 2, minutes = 0 prints 7200.
    • hours = 13, minutes = 47 prints 49620.

    Key takeaways

    • Convert every part to the same unit before adding.
    • Parentheses make Kotlin finish the sum before the last multiplication.
    • Two formulas can be equal: (hours * 60 + minutes) * 60 and hours * 3600 + minutes * 60 give the same number.
    • An Int holds far more seconds than a normal working time needs.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free