/******************************************************
* TMA02 q3a.cpp *
* This program accepts a time in 24-hour format and *
* returns the time in 12-hour format labeling with *
* AM or PM. Error checking for invalid input is *
* implemented. *
* Programming by Allen Lam (99043953) *
* Dec 2000 *
******************************************************/
#include <stdio.h>
int main(){
int hh, mm;
printf("Enter the time in 24-hour format (hh mm): ");
scanf("%d %d", &hh, &mm);
/* if hh and mm are out of possible ranges */
if (((mm<0)||(mm>=60)||(hh<0)||(hh>24)) || ((hh==24)&&(mm>0)))
printf("Error: invalid input.\n"); else
/* if it is 12:xx PM */
if (hh==12)
printf("The time in 12-hour format is 12:%02dPM", mm); else
/* if it is other PM hours */
if (hh>12)
printf("The time in 12-hour format is %d:%02dPM", hh-12, mm); else
/* otherwise, it is AM */
printf("The time in 12-hour format is %d:%02dAM", hh, mm);
fflush(stdin); getchar();
return 0;
}
back