142k views
1 vote
(Convert milliseconds to hours, minutes, and seconds) Write a function that converts milliseconds to hours, minutes, and seconds using the following header: def convertMillis(millis): The function returns a string as hours:minutes:seconds. For example, convertMillis(5500) returns the string 0:0:5, convertMillis(100000) returns the string 0:1:40, and convertMillis(555550000) returns the string 154:19:10. Write a test program that prompts the user to enter a value for milliseconds and displays a string in the format of hours:minutes:seconds. Sample Run Enter time in milliseconds: 555550000 154:19:10

User Fabasoad
by
4.1k points

1 Answer

4 votes

Answer:

I am writing the Python program. Let me know if you want the program in some other programming language.

def convertMillis(millis): #function to convert milliseconds to hrs,mins,secs

remaining = millis # stores the value of millis to convert

hrs = 3600000 # milliseconds in hour

mins = 60000 # milliseconds in a minute

secs = 1000 #milliseconds in a second

hours =remaining / hrs #value of millis input by user divided by 360000

remaining %= hrs #mod of remaining by 3600000

minutes = remaining / mins # the value of remaining divided by 60000

remaining %= mins #mod of remaining by 60000

seconds = remaining / secs

#the value left in remaining variable is divided by 1000

remaining %= secs #mod of remaining by 1000

print ("%d:%d:%d" % (hours, minutes, seconds))

#displays hours mins and seconds with colons in between

def main(): #main function to get input from user and call convertMillis() to #convert the input to hours minutes and seconds

millis=input("Enter time in milliseconds ") #prompts user to enter time

millis = int(millis) #converts user input value to integer

convertMillis(millis) #calls function to convert input to hrs mins secs

main() #calls main() function

Step-by-step explanation:

The program is well explained in the comments mentioned with each line of code. The program has two functions convertMillis(millis) which converts an input value in milliseconds to hours, minutes and seconds using the formula given in the program, and main() function that takes input value from user and calls convertMillis(millis) for the conversion of that input. The program along with its output is attached in screenshot.

(Convert milliseconds to hours, minutes, and seconds) Write a function that converts-example-1
User Mike McKay
by
5.0k points