In this tutorial, we will learn how to write a C program to convert 24 hour time format to 12 hour time format.
For example:
Input: 23 59
Output: 11:59 PM
C Program for 24 hour time to 12 hour time conversion
Let’s write a program for this conversion. The detailed explanation of the program is at the end of this article.
#include <stdio.h>
#include <string.h>
// Function to convert time from 24-hour to 12-hour format
void convert24To12(int hour, int minute) {
int hour12; // Variable to hold the converted hour
char period[3]; // Array to hold "AM" or "PM"
// Find the appropriate 12-hour format and period (AM/PM)
if (hour == 0) {
hour12 = 12;
strcpy(period, "AM");
} else if (hour < 12) {
hour12 = hour;
strcpy(period, "AM");
} else if (hour == 12) {
hour12 = 12;
strcpy(period, "PM");
} else {
hour12 = hour - 12;
strcpy(period, "PM");
}
// Print the time in 12-hour format
printf("%02d:%02d %s\n", hour12, minute, period);
}
int main() {
// Variables to hold user input
int hour, minute;
// Prompt user to enter time in 24-hour format
printf("Enter time in 24-hour format (HH MM): ");
scanf("%d %d", &hour, &minute); // Read user input
// Checks if the user entered time is in valid range
if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60) {
// Print error message if user input is found invalid
printf("Invalid data!\n");
} else {
// If entered time is valid then call convert24To12() for conversion
convert24To12(hour, minute);
}
return 0;
}
Output 1:
Enter time in 24-hour format (HH MM): 00 45
12:45 AM
Output 2:
Enter time in 24-hour format (HH MM): 12 00
12:00 PM
Output 3:
Enter time in 24-hour format (HH MM): 25 10
Invalid data!
Explanation of the program:
Header Files:
#include <stdio.h>
: It is included so that we can use input/output functions such asprintf
andscanf
.#include <string.h>
: It is included so that we can usestrcpy
function for copying strings.
Function Definition:
void convert24To12(int hour, int minute) {
int hour12;
char period[3];
convert24To12
: This function takes two integer argumentshour
andminute
, These arguments are in 24-hour format.hour12
: This variable will store hour in 12-hour format after conversion.period
: This array will store the “AM” or “PM” string.
Conversion Logic:
if (hour == 0) {
hour12 = 12;
strcpy(period, "AM");
} else if (hour < 12) {
hour12 = hour;
strcpy(period, "AM");
} else if (hour == 12) {
hour12 = 12;
strcpy(period, "PM");
} else {
hour12 = hour - 12;
strcpy(period, "PM");
}
- If
hour
is0
, thenhour12
is12
andperiod
isAM
. - If
hour
is less than12
, thenhour12
is equal tohour
andperiod
isAM
. - If
hour
is12
, thenhour12
is12
andperiod
isPM
. - If
hour
is greater than12
, thenhour12
is (hour
–12
) andperiod
isPM
.
Input Validation:
if (hour < 0 || hour >= 24 || minute < 0 || minute >= 60) {
printf("Invalid data!\n");
} else {
convert24To12(hour, minute);
}
return 0;
}
- The program checks if the entered
hour
is between0
and23
andminute
is between0
and59
. If not, it prints an error message “Invalid data!”. - If the input is valid, then it calls
convert24To12
function for conversion.
Leave a Reply