Computer Science
Write a program that reads two times in military format (0900, 1730) and prints the number of hours and minutes between the two times.
A sample run is being given below :
Please enter the first time : 0900
Please enter the second time : 1730
8 hours 30 minutes
Answer
ft = input("Please enter the first time : ")
st = input("Please enter the second time : ")
# Converts both times to minutes
fMins = int(ft[:2]) * 60 + int(ft[2:])
sMins = int(st[:2]) * 60 + int(st[2:])
# Subtract the minutes, this will give
# the time duration between the two times
diff = sMins - fMins;
# Convert the difference to hours & mins
hrs = diff // 60
mins = diff % 60
print(hrs, "hours", mins, "minutes")
Output
Please enter the first time : 0900
Please enter the second time : 1730
8 hours 30 minutes
Please enter the first time : 0915
Please enter the second time : 1005
0 hours 50 minutes
Related Questions
Write a program that reads an integer N from the keyboard computes and displays the sum of the numbers from N to (2 * N) if N is nonnegative. If N is a negative number, then it's the sum of the numbers from (2 * N) to N. The starting and ending points are included in the sum.
Write a program that reads a date as an integer in the format MMDDYYYY. The program will call a function that prints print out the date in the format <Month Name> <day>, <year>.
Sample run :
Enter date : 12252019 December 25, 2019
Write a program that prints a table on two columns — table that helps converting miles into kilometres.
Write another program printing a table with two columns that helps convert pounds in kilograms.