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

Python

Python Funda

28 Likes

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

Answered By

14 Likes


Related Questions