Object

Write a program to calculate the difference between two times using a function.

Algorithm

  1. Declare a function int getmins(), which does not accept arguments but returns an integer value.
    The Function main
  2. Declare integer variables time1 and time2.
  3. Call the function getmins() and store the value it returns in time1.
  4. Again call the function getmins() and store the value it returns in time2.
  5. Write time2 ­− time1.
  6. Exit.
    Defining the Function int getmins()
  7. Declare integer variables hrs and mins.
  8. Declare character variable separator.
  9. Read hrs, separator, and mins.
  10. Return(hrs*60+mins).
  11. 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

Design a site like this with WordPress.com
Get started