Code:
#include <stdio.h>
struct time1
{
int h1,h2;
int m1,m2;
int s1,s2;
}time;
main( )
{
int i,s1,s2,d1,d2,d3;
printf("Enter the first time in Hrs, mins and seconds.\n");
scanf("%d %d %d",&time.h1,&time.m1,&time.s1);
printf("Enter the second time in Hrs, mins and seconds.\n");
scanf("%d %d %d",&time.h2,&time.m2,&time.s2);
printf("Choose the operation:\n1. Read\n2. Display\n3. Add\n4. Subtract\n5. Exit.\n");
scanf("%d",&i);
switch(i)
{
case 1:
{
printf("You have entered:\n1. %dHr %dMin %dSeconds.\n2. %dHr %dMin %dSeconds.",time.h1,time.m1,time.s1,time.h2,time.m2,time.s2);
break;
}
case 2:
{
printf("You have entered:\n1. %dHr %dMin %dSeconds.\n2. %dHr %dMin %dSeconds.",time.h1,time.m1,time.s1,time.h2,time.m2,time.s2);
break;
}
case 3:
{
s1=time.s1+time.s2;
s2=time.m1+time.m2;
if(time.s1+time.s2>60)
{
s1=time.s1+time.s2;
s1=s1-60;
s2++;
}
if(time.m1+time.m2>60)
{
s2=s2-60;
time.h1++;
}
printf("Addition:\n%dHr %dMin %dSeconds",time.h1+time.h2,s2,s1);
break;
}
case 4:
{
if(time.h1>time.h2)
{
d1=time.h1-time.h2;
d2=time.m1-time.m2;
d3=time.s1-time.s2;
if(d2<0)
{
d2=d2+60;
d1=d1-1;
}
if(d3<0)
{
d3=d3+60;
d2=d2-1;
}
}
if(time.h2>time.h1)
{
d1=time.h2-time.h1;
d2=time.m2-time.m1;
d3=time.s2-time.s1;
if(d2<0)
{
d2=d2+60;
d1=d1-1;
}
if(d3<0)
{
d3=d3+60;
d2=d2-1;
}
}
if(time.h1==time.h2)
{
d1=0;
d2=time.m1-time.m2;
d3=time.s1-time.s2;
if(d2<0)
{
d2=0-d2;
}
if(d3<0)
{
d3=d3+60;
d2--;
}
}
if(time.m1==time.m2)
{
d2=0;
}
if(time.s1==time.s2)
{
d3=0;
}
printf("Subtraction - %dHr %dMin %dSeconds.",d1,d2,d3);
break;
}
}
}