C++ program to convert/normalize the given time into standard form
Last Updated :
06 Sep, 2021
Given three variables that represent the hours, minutes, and the second, the task is to write a C++ program to normalize/convert the given time into standard form i.e., HR: MIN: SEC format.
Examples:
Input: HR = 5, MIN = 125, SEC = 130
Output: 7:7:10
Approach: To normalize the given time, below are the functions/steps using the class:
- 3 variables are defined to store the value of hours, minutes, and second respectively.
int HR, MIN, SEC;
where HR represents hours,
MIN represents minutes and
SEC represents seconds
- setTime() function to set values of HR, MIN and SEC:
void setTime(int x, int y, int z)
{
x = HR;
y = MIN;
z = SEC;
}
- showTime() to display the time:
void showTime()
{
cout << HR << ":" << MIN << ":" << SEC;
}
- normalize() function to convert the resultant time into standard form. Normalizing the time by converting it according to the standard time format as:
void normalize()
{
MIN = MIN + SEC/60;
SEC = SEC%60;
HR = HR + MIN/60;
MIN = MIN%60;
}
Illustration: For Example Time: 5: 125: 130
- To normalize the time:
- First, divide the SEC by 60 and to find the minutes and add it into MIN,
- so, 125+(130/60) =125+2 = 127.
- Then take the modulo of SEC to find the seconds 130 % 60 = 10.
- Now, divide MIN by 60 find the hours, and add it into HR, 5+(127/60) = 5+2= 7.
- Lastly take modulus of MIN to find the minutes, 127 % 60 = 7, So, HR= 7, MIN= 7, SEC= 10.
- Therefore, the result is 7: 7: 10
Below is the implementation of the above approach:
C++
// C++ program to normalize the given
// time into standard form
#include <conio.h>
#include <iostream>
using namespace std;
// Time class
class Time {
private:
// Instance variables
int HR, MIN, SEC;
public:
void setTime(int, int, int);
void showTime();
void normalize();
};
// Function that sets the given time
void Time::setTime(int h, int m, int s)
{
HR = h;
MIN = m;
SEC = s;
}
// Function to show the given time
void Time::showTime()
{
cout << endl
<< HR << ":"
<< MIN << ":" << SEC;
}
// Function to normalize the given
// time into standard form
void Time::normalize()
{
// Convert the time into the
// specific format
MIN = MIN + SEC / 60;
SEC = SEC % 60;
HR = HR + MIN / 60;
MIN = MIN % 60;
}
// Driver Code
int main()
{
// Object of class Time
Time t1;
t1.setTime(5, 125, 130);
t1.showTime();
// Normalize the time
t1.normalize();
t1.showTime();
return 0;
}
Output:
Similar Reads
C++ Program to Convert Scientific Notation to Decimal Here, we will see how to convert scientific notations to decimals using a C++ program. Example: 1e9 = 1000000000 1e9 = 1000000000.000000 Calculate power using the pow(a,b) function Header File used: <math.h> pow also referred to as the power of function. Here, pow(a,b) refers to a power b. Let
3 min read
Program for converting hours into minutes and seconds Given an integer n which is the number of hours, the task is to convert it into minutes and seconds. Examples: Input: 5 Output: Minutes = 300, Seconds = 18000 Input: 2 Output: Minutes = 120, Seconds = 7200 Approach: h hours = h * 60 minutes as 1 hour = 60 minutes.Similarly h hours = h * 3600 seconds
4 min read
How to Get Time in Milliseconds in C++? In programming measuring accurate time is a common requirement for most software development packages. In C++, we have the std::chrono library with various functions for durations, time, and clocks. In this article, we will learn how to get time in milliseconds in C++. For Example, Input:Current sys
2 min read
C++ Program To Find Simple Interest What is 'Simple Interest'? Simple interest is a quick method of calculating the interest charge on a loan. Simple interest is determined by multiplying the daily interest rate by the principal by the number of days that elapse between payments. Simple Interest formula: Simple interest formula is giv
2 min read
Calculate the IST : Indian Standard Time Given two integer H and R where H is the time in hours at a place X and R is the distance in degrees from place X to India, the task is to find the current time in IST.UTC (Coordinated Universal Time) is a 24-hour time standard that is used to synchronize world clocks.Examples: Input: H = 24, R = 82
4 min read