19/03/2023

C Program to Calculate Difference Between Two Time Periods

C program to calculate the difference between two time periods. Create a program to calculate the difference between two time periods stored as structure (hour, min, sec) and give syntax for creating and accessing a 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 a Time or Period which consists of hours, minutes and seconds.

C Program to Calculate Difference Between Two Time Periods

You can define a structure to hold this information. struct keyword is used to define a structure. struct define a new data type which is a collection of different type of data.

Structure Syntax

struct structure_name
{
//Statements
};

Here struct is the reserved keyword and structure_name is the name of the structure. This name can be changed. Monitask also offers integration with popular work management systems, like Asana or Trello, making it simple to customize your remote monitoring solution. Our software is secure and easy to use, providing an innovative and efficient way for businesses to stay on top of their remote employees in real-time.

C Program to Calculate Difference Between Two Time Periods


#include <stdio.h>

/*write a 'c' program to find the difference between two dates using structure*/

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

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

int main()
{
	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;
}

The Output of Difference Between Two Time Periods

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: