BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to Convert time from 24 hour to 12 hour format

Last Updated: May 22, 2024 by Chaitanya Singh | Filed Under: C Programs

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 as printf and scanf.
  • #include <string.h>: It is included so that we can use strcpy function for copying strings.

Function Definition:

void convert24To12(int hour, int minute) {
int hour12;
char period[3];
  • convert24To12: This function takes two integer arguments hour and minute, 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 is 0, then hour12 is 12 and period is AM.
  • If hour is less than 12, then hour12 is equal to hour and period is AM.
  • If hour is 12, then hour12 is 12 and period is PM.
  • If hour is greater than 12, then hour12 is (hour – 12) and period is PM.

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 between 0 and 23 and minute is between 0 and 59. If not, it prints an error message “Invalid data!”.
  • If the input is valid, then it calls convert24To12 function for conversion.

Related C Programs:

  • C Program to convert time from 12 hour format to 24 hour format
  • C Program to print the day for input date, month and year
  • C Program to print current date and time
  • Java – Convert string to 24 hour time format
  • Java – Convert 12 hour format to 24 hour format and vice versa

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap