Modulus opereator

Feb 10, 2017 at 5:23am
Hi new at c++ have a problem, Using modulus operator write a C++ program that will convert military time to standard time(am/pm).
Appreciate any help!
Feb 10, 2017 at 6:49am
Given a military time, m

The corresponding civilian time is given by:
floor ((m mod 1200) / 100) hours,
m mod 100 minutes.

Example:
Currently it is 0142 hours where I live. 1:42 AM.

floor (142 mod 1200 / 100) = floor(142 / 100) = 1 hour
142 mod 100 = 42 minutes

The math works equivalently for 1342 hours, or 1:42 PM.

Note:
In C++, a integer literal which appears with a leading zero is considered an octal (base 8) literal. Be careful of this in case you hard-code the current time into your program with a leading zero.
Feb 10, 2017 at 7:20am
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <iomanip>

int main()
{
    int hour = 0;
    int minute = 0;
    
    for (int i = 0; i < 2400; i++)
    {
        hour = i/100;
        minute  = i % 100;
        
        if(minute < 60)
        {
            std::cout
            << std::setfill('0') << std::setw(2) << hour
            << std::setfill('0') << std::setw(2) << minute << " hrs\t"
            
            << hour % 12 << ':'
            << std::setfill('0') << std::setw(2) << minute << (hour < 12 ?  " AM": " PM") << '\n';
        }
    }
    
    return 0;
}
Last edited on Feb 10, 2017 at 7:22am
Topic archived. No new replies allowed.