Hello. I have an exercisse to convert from 24-hour to 12-hour clock. code:
/*ALGORITHM
READ INPUT VALUE IN 24-HOUR FORMAT
DISPLAY THE EQUIVALENT 12-HOUR CLOCK WITH THE PERIODS AM OR PM
*/
#include<stdio.h>
#include<conio.h>
int InputValid24Hour(void);
void Output12HourClock(int);
void main() {
int data;
data = InputValid24Hour();
Output12HourClock(data);
_getch();
}
int InputValid24Hour(void) {
int input24hour;
int First2Digit, Last2Digit;
printf("Enter time in 24-hour format: \r\n"); //ask for input
do {
scanf_s("%d", &input24hour); //read input
First2Digit = input24hour / 100; //get first 2 digits
Last2Digit = input24hour % 100; //get last 2 digits
if ((First2Digit < 0) || (First2Digit > 24) || (Last2Digit >= 60))
printf("\r\nInvalid 24-hour clock..");
} while ((First2Digit < 0) || (First2Digit > 24) || (Last2Digit >= 60));
return(input24hour);
}
void Output12HourClock(int i) { //int i value from InputValid24Hour() see main()
int First, Last;
First = i / 100;
Last = i % 100;
if (First > 12) {
First = First - 12;
printf("%02d = %02d:%02d pm\r\n", i, First, Last);
}
else if (First == 00) {
First = 12;
printf("%02d = %02d:%02d am\r\n", i, First, Last);
}
else {
printf("%02d = %02d:%02d am\r\n", i, First, Last);
}
}
Output : http://imgur.com/a/OtfJL
the problem'ss at 24, it doessn't sshow 00 when it sshould sso it sshould be 0024 = 12:24 am; how do i get 00 to be displayed?
thankss