Programming Examples

C program to addition of two times using structure


Define a structure Time having integer data members hour, minute, second. Write a program to enter two variables of the type Time and then add these two variables and store the result into third variable. Also validate the second and minute of the result and print it.

Solution

#include<stdio.h>
struct Time
{
	int hour,minute,second;
};
int main()
{
	struct Time t1,t2,t3;
	printf("Enter the Time 1 (Hour,minute and Second)");
	scanf("%d%d%d",&t1.hour,&t1.minute,&t1.second);
	printf("Enter the Time 2 (Hour,minute and Second)");
	scanf("%d%d%d",&t2.hour,&t2.minute,&t2.second);
	t3.hour=t1.hour+t2.hour+(t1.minute+t2.minute+(t1.second+t2.second)/60)/60;
	t3.minute=(t1.minute+t2.minute+(t1.second+t2.second)/60)%60;
	t3.second=(t1.second+t2.second)%60;
	printf("\n Time 1 %d : %d : %d",t1.hour,t1.minute,t1.second);
	printf("\n Time 2 %d : %d : %d",t2.hour,t2.minute,t2.second);
	printf("\n Time 3 %d : %d : %d",t3.hour,t3.minute,t3.second);
	return 0;
}
Output

Enter the Time 1 (Hour,minute and Second)3 40 50

Enter the Time 2 (Hour,minute and Second)2 45 50


 Time 1 3 : 40 : 50

 Time 2 2 : 45 : 50

 Time 3 6 : 26 : 40

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz