Write a program, which contents a structure to represent time in hours(0-23), minutes(0-59) and second (0-59). The program should accept two times from user and display the duration time between the two times.
source file name: timediff.C
#include<stdio.h>
struct time
{
int h,m,s;
};
int main()
{
int validtime(struct time t);
struct time t1,t2;
int flag=0;
int hrs,min,sec;
//clrscr();
printf("\n enter first time interval H:M:S");
scanf("%d%d%d",&t1.h,&t1.m,&t1.s);
printf("\n enter second time interval H:M:S");
scanf("%d%d%d",&t2.h,&t2.m,&t2.s);
flag=validtime(t1);
flag=validtime(t2);
if(flag)
{
printf("\n Invalid time intervals ");
printf(" now program ends..");
goto p;
}
if(t2.s<t1.s)
{
sec=t2.s+60-t1.s;
t2.m=t2.m-1;
}
else
sec=t2.s-t1.s;
if(t2.m<t1.m)
{
min=t2.m+60-t1.m;
t2.h=t2.h-1;
}
else
min=t2.m-t1.m;
hrs=t2.h-t1.h;
printf("\n Time difference between given intervals: %d : %d : %d",hrs,min,sec);
p:
//getch();
return 0;
}
int validtime(struct time t)
{
int f=0;
if(t.h>24 || t.h<0)
{
printf("\n invalid hours");
f=1;
}
if(t.m>60 || t.m<0)
{
printf("\n invalid minutes");
f=1;
}
if(t.s>60 || t.s<0)
{
printf("\n invalid seconds");
f=1;
}
return f;
}
output:
0 Comments
Post a Comment