Search⌘ K
AI Features

Challenge: Calculate Hours, Minutes, and Seconds

Explore how to use C++ operators to convert a given total number of seconds into hours, minutes, and seconds. This lesson helps you practice variable manipulation and arithmetic operations to solve a common programming challenge. By completing this exercise, you will strengthen your understanding of operators in C++ and improve your problem-solving skills.

Problem statement

In this challenge, you are given the total number of seconds, and your task is to display the number of hours, minutes, and seconds in it.

1 minute=60 seconds1\ minute = 60\ seconds

1 hour=60 minutes1\ hour= 60\ minutes

Input

We have already initialized the variable total_seconds at the backend.

int total_seconds = 3870;

Output

Your code should have the following output:

hours = 1
minutes = 4
seconds = 30

📝 Please use the variables total_seconds for the input and hours, minutes, and seconds for the output, or your code will not execute.

Coding exercise

Before diving directly into the solution, try to solve it yourself.Then, check if your code passes all the test cases.

Good luck! 👍

C++
/* We have already declared the input variable total_seconds
at the backend.
int total_seconds;
Display the total number of hours, minutes, and seconds in it.
Store your output in hours, minutes, and seconds variables.
*/
int hours, minutes, seconds;
// Write your code here

🎉Congratulations! If you have solved the problem, give yourself a treat.

In case you are stuck, you can go over the solution review in the next lesson.