G c 24-hour time (also known in the U. S. as military time) is widely used around the world. Time is expressed as hours since midnight. The day starts at 00:00, and ends at 23:59. Write a program that converts am/pm time to 24-hour time. The input is two numbers and a string. If the input is 2 30 pm, the output should be 14:30. If the input is 12 01 am, the output should be 00:01. Hints: Think of how each hour should be handled. 12 00 am -12 59 am becomes what
45v
because 60÷12=5
5x9=45
see explaination
Explanation:
#include<string>
using namespace std;
int main(){
int hr, min; string meridian;
int m_hr, m_min;
do{
cout<<"Enter hour [0-12]: "; cin >> hr;
}while(hr<1 || hr>12);
do{
cout<<"Enter minutes [0-59]: "; cin >> min;
}while(min<0 || min>59);
do{
cout<<"Enter am or pm: "; cin >> meridian;
}while(meridian.compare("am")!=0 && meridian.compare("pm")!=0);
int abs_time = 0;
if(meridian.compare("am")==0){
abs_time = hr*60 + min;
if(hr==12){
abs_time -= hr*60;
}
}else{
abs_time = 12*60 + hr*60 + min;
}
m_hr = abs_time/60;
m_min = abs_time%60;
cout<<"Military Time: "<<(m_hr<10?"0":"")<<m_hr<<":"
<<(m_min<10?"0":"")<<m_min<<endl ;
}