Time Conversion

Problem Statement

You are given time in AM/PM format. Convert this into a 24 hour format.

Note Midnight is 12:00:00AM or 00:00:00 and 12 Noon is 12:00:00PM.

Input Format

Input consists of time in the AM/PM format i.e. hh:mm:ssAM or hh:mm:ssPM
where
01hh12
00mm59
00ss59

Output Format

You need to print the time in 24 hour format i.e. hh:mm:ss
where
00hh23
00mm59
00ss59

Sample Input

07:05:45PM

Sample Output

19:05:45

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int h,m,s;
    char c;
    scanf("%d:%d:%d%c",&h,&m,&s,&c);
    if(c=='P')
        if(h==12)
            cout<<12;
        else cout<<h+12;
    else if(h<10)
            cout<<"0"<<h;
    else if(h==12)
        cout<<"00";
    else cout<<h;    
    if(m<10)
        cout<<":0"<<m;
    else cout<<":"<<m;
    if(s<10)
        cout<<":0"<<s;
    else cout<<":"<<s;    
    
    return 0;
}


Leave a Reply