English

Write a program to find difference between two times with hours, minutes and seconds.

Advertisements
Advertisements

Question

Write a program to find difference between two times with hours, minutes and seconds.

Code Writing
Advertisements

Solution

Convert both times into seconds, find the difference, and convert the result back into hours, minutes and seconds.

 
hours1 = int(input("Enter first time hours: "))
minutes1 = int(input("Enter first time minutes: "))
seconds1 = int(input("Enter first time seconds: "))
hours2 = int(input("Enter second time hours: "))
minutes2 = int(input("Enter second time minutes: "))
seconds2 = int(input("Enter second time seconds: "))

time1 = hours1 * 3600 + minutes1 * 60 + seconds1
time2 = hours2 * 3600 + minutes2 * 60 + seconds2
difference = abs(time1 - time2)

hours = difference // 3600
difference %= 3600
minutes = difference // 60
seconds = difference % 60

print("Difference =", hours, "hours", minutes, "minutes", seconds, "seconds")
 
shaalaa.com
  Is there an error in this question or solution?
Chapter 1: Review of Phython - EXERCISE [Page 27]

APPEARS IN

CBSE Computer Science with Python [English] Class 12
Chapter 1 Review of Phython
EXERCISE | Q 19. | Page 27
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×