24.1k views
11 votes
People find it easier to read time in hours, minutes, and seconds rather than just seconds. Write a program that reads in seconds as input, and outputs the time in hours, minutes, and seconds

1 Answer

9 votes

Answer:

seconds = int(input("Enter time in seconds: "))

hours = int(seconds / 3600)

minutes = int((seconds - (hours * 3600)) / 60)

seconds = seconds - ((hours * 3600) + (minutes * 60))

print("{} hour(s) {} minute(s) {} second(s)".format(hours, minutes, seconds))

Step-by-step explanation:

*The code is in Python.

Ask the user to enter the time in seconds

Calculate the hours, divide the seconds by 3600 and cast it to the integer

Calculate the minutes, subtract the corresponding seconds in hours you previously found, divide it by 60 and cast the result to the integer

Calculate the seconds, subtract the corresponding seconds in hours and minutes you previously found, the remaining is the seconds you have

Print the hours, minutes, and seconds

For example, if the user enters 7310 as seconds,

hours will be int(7310/3600) = 2

minutes will be int((7310 - (2*3600)) / 60) = 1

seconds will be 7310 - ((2*3600) + (1*60)) = 50

User FredyC
by
3.9k points