Object
Write a program to calculate the difference between two times using a function.
Algorithm
- Declare a function int getmins(), which does not accept arguments but returns an integer value.
The Function main - Declare integer variables time1 and time2.
- Call the function getmins() and store the value it returns in time1.
- Again call the function getmins() and store the value it returns in time2.
- Write time2 − time1.
- Exit.
Defining the Function int getmins() - Declare integer variables hrs and mins.
- Declare character variable separator.
- Read hrs, separator, and mins.
- Return(hrs*60+mins).
- Exit.
Flowchart

C++ Source Code
// program 78
#include<iostream>
using namespace std;
int getmins();
int main()
{
int time1, time2;
cout << "Enter the first time (in 0:00 form): ";
time1 = getmins();
cout << "Enter the later second time (in 0:00 form): ";
time2 = getmins();
cout << "The difference between two times is: " << time2 - time1 << " minutes";
return 0;
}
int getmins()
{
int hrs, mins;
char separator;
cin >> hrs >> separator >> mins;
return (hrs * 60 + mins);
}
C Source Code
/*program 78*/
#include<stdio.h>
int getmins(void);
int main()
{
int time1, time2;
printf("Enter the first time (in 0:00 form): ");
time1 = getmins();
printf("Enter the later second time (in 0:00 form): ");
time2 = getmins();
printf("The difference between two times is: %d minutes", time2-time1);
return 0;
}
int getmins(void)
{
int hrs, mins;
scanf("%d:%d", &hrs, &mins);
return(hrs*60+mins);
}
Output
Enter the first time (in 0:00 form): 5:30
Enter the later second time (in 0:00 form): 7:34
The difference between two times is: 124 minutes
