Time Conversion Hackerrank Solution C++ Program
ADVERTISEMENTS
Time conversion hackerrank solution C++ program.
In this article, you will learn how to make a time conversion hackerrank solution C++ program.
Example
INPUT TIME::
09:30:30PM
CONVERTED TIME:: 21:30:30
You should have knowledge of the following topics in c++ programming to understand these programs:
- C++ 
main()function 
Source Code
// Time Conversion Hackerrank Solution C++ Program
#include <iostream>
using namespace std;
int main() {
    char timestamp[11]="";
    int hr=0;
    
    cout << "INPUT TIME::\n";
    cin >> timestamp;
    
    if (timestamp[8]=='P') {
        hr = 10*(timestamp[0]-'0')+(timestamp[1]-'0');
        if (hr < 12) hr += 12;
    }
    else {
        hr = 10*(timestamp[0]-'0')+(timestamp[1]-'0');
        if (hr==12) hr=0;
    }
    
    timestamp[0] = hr/10 + '0';
    timestamp[1] = hr%10 + '0';
    timestamp[8] = '\0';
    timestamp[9] = '\0';
    
    // It will print the final output
    cout << "\nCONVERTED TIME:: " << timestamp;
    return 0;
}
Output
INPUT TIME::
09:30:30PM
CONVERTED TIME:: 21:30:30
Explanation
In this given program, we have taken input 09:30:30PM from the user then we make calculations on this input to find the time format. Then It will return the 21:30:30 the output of the program.