Sunday, 24 April 2022

7. Define a structure by name time with members seconds, minutes and hours of int type. A variable of the structure would thus represent time. If time1 and time2 are two variables of the structure type, write a program to find the difference of two times using a function.

 


#include<stdio.h>
#include<conio.h>

struct time
{
    int sec,min,h;
}t[2];

void main()
{
    int i,s,m,hh;
    clrscr();

    for(i=0; i<2; i++)
    {
        printf("enter time %d: ",i+1);
        printf("\n\nenter hour for time %d: ",i+1);
        scanf("%d",&t[i].h);
        if(t[i].h > 24)
        {
            printf("\n!! please enter less than 24 !!");
            getch();
            exit(0);
        }

        printf("\nenter minute for time %d: ",i+1);
        scanf("%d",&t[i].min);
        if(t[i].min > 60)
        {
            printf("\n!! please enter less than 60 !!");
            getch();
            exit(0);
        }

        printf("\nenter second for time %d: ",i+1);
        scanf("%d",&t[i].sec);
        if(t[i].sec > 60)
        {
            printf("\n!! please enter less than 60 !!");
            getch();
            exit(0);
        }

        printf("\n\n");
    }

    for(i=0; i<2; i++)
    {
        printf("\ntime %d= %d : %d : %d",i+1,t[i].h,t[i].min,t[i].sec);
    }


    if(t[0].h > t[1].h)
    {
        hh = t[0].h - t[1].h;
    }
    else
    {
        hh = t[1].h - t[0].h;
    }

    if(t[0].min > t[1].min)
    {
        m = t[0].min - t[1].min;
    }
    else
    {
        m = t[1].min - t[0].min;
    }

    if(t[0].sec > t[1].sec)
    {
        s = t[0].sec - t[1].sec;
    }
    else
    {
        s = t[1].sec - t[0].sec;
    }

    printf("\n\ndifference between two time= %d : %d : %d",hh,m,s);

    getch();
}

/*  output:

enter time 1:

enter hour for time 1: 12

enter minute for time 1: 35

enter second for time 1: 24


enter time 2:

enter hour for time 2: 8

enter minute for time 2: 59

enter second for time 2: 46



time 1= 12 : 35 : 24
time 2= 8 : 59 : 46

difference between two time= 4 : 24 : 22

*/

No comments:

Post a Comment

python programs

1. sum of two number