Preview:
#include <iostream>
#include <string>
using namespace std;


int main()
{
    
    int hour24, minutes24;
    int hour12, minutes12;
    string period;
    char value;

    cout << "Enter the time in a 24-hour format \n";
    cin >> hour24 >> value >> minutes24;

    minutes12 = minutes24;

    if (hour24 >= 0 && hour24 <= 11) {
        period = "AM";
        if (hour24 == 0) {
            hour12 = 12;
        }
        else
            hour12 = hour24;
    }
    else
    {
        period = "PM";
        if (hour24 == 12) {
            hour12 = hour24;
        }
        else
            hour12 = hour24 - 12;
    }

    cout << hour24 << value << minutes24 << " is " << hour12 << value << minutes12<<" " << period << endl;

    return 0;
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter