- 3

Write a program that converts from 24-hour notation to 12-hour notation. (Solved)

The input is given as two integers. There should be at least three functions, one for input, one to do the conversion and one for output.Record the AM/PM information as a value of type char, ‘A’ for AM and char ‘P’ for PM. Thus, the function for doing the conversions will have a call-by-reference formal parameter of type char to record whether it is AM or PM. Input: 14:30:19 Output: 2:30:19 PM

25th Jun 2021, 2:38 AM
Karthik
2 Réponses
+ 1
#include <iostream> using namespace std; void input(int& hrs,int& min,char& mer) { do { cout <<"Enter hours: "; cin >> hrs; if(hrs>23) { cout <<"Enter a value between 0 and 23"<<endl; } }while(hrs>23); do { cout <<"Enter minutes: "; cin >>min; if(min>59) { cout <<"Enter a value between 0 and 59"<<endl; } }while(min>59); } void conv24to12(int& hrs,int& min,char& mer) { if(hrs>12) { hrs=hrs-12; mer='p'; } else if(hrs==12) mer='p'; else mer='a'; } void output(int& hrs,int& min,char& mer) { if(mer=='p') { if(min<10) { cout <<hrs<<":0"<<min<<" P.M."; } else cout <<hrs<<":"<<min<<" P.M."; } else { if(min < 10) { cout <<hrs<<":0"<<min<<" A.M."; } else cout <<hrs<<":"<<min<<" A.M."; } } int main() { int hrs, min; char mer, re; do { input(hrs,min,mer); conv24to12(hrs,min,mer); output(hrs,min,mer); cout <<"\nEnter A to run again,any other key to exit: "; cin >>re; } while(re=='a' || re=='A'); return 0; }
25th Jun 2021, 5:12 AM
Karthik