Programming Examples

Python program for addition of two times in hour minute and second format


Write a python program which accept two times (HH:MM:SS) as input and displays the total time after adding both the entered times.

Sample :

Input:

Enter Time 1 :

Hour : 2

Minute : 40

Second : 45

Enter Time 2 :

Hour : 1

Minute : 35

Second : 40

Output:

Time 1 : 2  Hour, 40  Minute and  45  Second

Time 2 : 1  Hour, 35  Minute and  40  Second

Total Time  : 4  Hour & 16  Minute and  25 Second

Solution

print("Enter Time 1 :")
h1=int(input("Hour : "))
m1=int(input("Minute : "))
s1=int(input("Second : "))
print("Enter Time 2 :")
h2=int(input("Hour : "))
m2=int(input("Minute : "))
s2=int(input("Second : "))
h3=h1+h2+(m1+m2+(s1+s2)//60)//60
m3=(m1+m2+(s1+s2)//60)%60
s3=(s1+s2)%60
print("Time 1 :",h1," Hour,",m1," Minute and ",s1," Second")
print("Time 2 :",h2," Hour,",m2," Minute and ",s2," Second")
print("Total Time  :",h3," Hour &",m3," Minute and ",s3,"Second")
Output/ Explanation:

Enter Time 1 :

Hour : 3

Minute : 45

Second : 50

Enter Time 2 :

Hour : 1

Minute : 27

Second : 45

Time 1 : 3  Hour, 45  Minute and  50  Second

Time 2 : 1  Hour, 27  Minute and  45  Second

Total Time  : 5  Hour & 13  Minute and  35 Second

Latest Current Affairs 2024 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad