4/15/2016

C Program To Find Difference Between Two Dates Using Structure

Problem:- C Program to Calculate Difference Between Two Time Period ( HH: MM: SS ) Using Structure or c program to find difference between two dates using structure or C Program to Calculate Difference Between Two Time Periods or C calculate difference between two-time strokes or How to add hours: minutes: seconds (hh: mm: ss) keeping or Write a C program to convert time in seconds to the time in hours or Print difference of two times in hours, minutes and seconds in c or C Program to compute the difference between two dates or C program to find the number of days between two dates or C Program to Calculate Difference Between Two Time Periods.

Check This:- Hacker rank solution for Strings, Classes, STL, Inheritance in C++.

What Is Structure

The structure is a user-defined data type in C which allows you to combine different data types Suppose you want to store a record of Student which consists of student name, address, roll number and age. You can define a structure to hold this information.

Defining a structure

struct keyword is used to define a structure. struct define a new data type which is a collection of different type of data.

Syntax :

struct structure_name
{
//Statements
};

Also Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.

Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date for a new post.

Solution:-

#include <stdio.h>


struct TIME
{
int seconds;
int minutes;
int hours;
};

void Difference(struct TIME t1, struct TIME t2, struct TIME *diff);

int main()
{
//Ghanendra Yadav

struct TIME t1,t2,diff;

printf("Enter start time: \n");
printf("Enter hours, minutes and seconds respectively: ");
scanf("%d%d%d",&t1.hours,&t1.minutes,&t1.seconds);
printf("Enter stop time: \n");
printf("Enter hours, minutes and seconds respectively: ");

scanf("%d%d%d",&t2.hours,&t2.minutes,&t2.seconds);

Difference(t1,t2,&diff);

printf("\nTIME DIFFERENCE: %d:%d:%d - ",t1.hours,t1.minutes,t1.seconds);
printf("%d:%d:%d ",t2.hours,t2.minutes,t2.seconds);
printf("= %d:%d:%d\n",diff.hours,diff.minutes,diff.seconds);

return 0;
}

void Difference(struct TIME t1, struct TIME t2, struct TIME *differ)
{
if(t2.seconds>t1.seconds)
{
--t1.minutes;
t1.seconds+=60;
}

differ->seconds=t1.seconds-t2.seconds;

if(t2.minutes>t1.minutes)
{
--t1.hours;
t1.minutes+=60;
}

differ->minutes=t1.minutes-t2.minutes;
differ->hours=t1.hours-t2.hours;
}

Output:-


C Program To Find Difference Between Two Dates Using Structure


You May Also See


1. C Program to Reverse a Number Using While Loop

2. C Program to Check Whether a Number is A Palindrome or Not using While Loop

3. C Program to Count Number of Digits of an Integer Using While Loop

4. C Program For Find A Generic Root Of Number Using While Loop

5. C Program To Print A Calender Taking Input From User Using Loop

6. C Program To Generate IP (Internet Protocol) Addresses Using For Loop

7. C PROGRAM TO CHECK WHETHER ENTERED NUMBER IS DIVISIBLE BY 11 BY USING OLD TRICK (VEDIC MATH)

8. C Program For Sort A Float Array In Ascending And Descending Order Using For Loop




No comments:

Post a Comment