This is a C Program to convert 12-hour AM-PM time format to 24-hour Military time format.
Given a time in 12-hour AM/PM format, convert it to military (24-hour) time. User has to take input as a string containing a time in 12-hour clock format (i.e.: hh:mm:ssAM or hh:mm:ssPM), where 01 ≤ hh ≤12 and 00 ≤ mm,ss ≤59.
Input:- 09:15:55PM
Output:- 21:15:55
Input:- 12:00:00AM
Output:- 00:00:00
Input:- 03:55:50AM
Output:- 03:55:50
1. Take input as taken above in sample inputs. (“ %d:%d:%d%s ”, &hh,&mm,&ss,a) or you can provide inputs to ‘hh’, ‘mm’, ‘ss’ and ‘a’ separately.
2. Check and compare if the string ‘a’ at the end of input is ‘AM’ or ‘PM’.
3. Check the value of hh, and solve accordingly.
Here is source code of the C Program for conversion of 12-hour AM-PM time format to 24-hour military time format. The program is successfully compiled and tested using Codeblocks gnu/gcc compiler on windows 10. The program output is also shown below.
/* C Program for converting 12-hour time format to 24-hour time format. */
#include<stdio.h>
#include<string.h>
int main()
{
int hh, mm, ss;
char a[3];
printf("Enter hours 'hh' \t");
scanf("%d", &hh);
printf("Enter minutes 'mm' \t");
scanf("%d", &mm);
printf("Enter seconds 'ss' \t");
scanf("%d", &ss);
printf("Enter string 'am' or 'pm' \t");
scanf("%s", &a);
/*
* user is allowed to enter time only in 12-hour format
* so that 'hh' cannot be greater than 12.
*/
if(hh <= 12 && mm <= 59 && ss <= 59)
{
if((strcmp(a,"PM") == 0) || (strcmp(a,"pm") == 0)
&& (hh != 0) && (hh != 12))
{
hh = hh + 12;
}
if((strcmp(a,"AM") == 0) || (strcmp(a,"am") == 0) && (hh == 12))
{
hh = 0;
}
printf("The obtained 24-hour format of input is \t");
printf("%02d:%02d:%02d", hh, mm, ss);
printf("\n\n");
}
else
{
printf("Provide the correct inputs.");
}
return 0;
}
1. The user will give input in 12-hour format, which will contain 4 variables, hh for hours, mm for minutes, ss for seconds and a string ‘a’ for denoting ‘AM’ or ‘PM’.
2. After taking the input the user will check if it is ‘PM’ and value of ‘hh’ is anything apart from 00 or 12, it’ll be directly added by 12 and mm,ss will remain same. For example, if the user inputs hh as 05 ‘PM’, in 24-hour format 05 pm = 17 which is nothing but 05+12.
3. But if it is ‘AM’ and value of hh is 12, the value of hh will be made = 0 because after 23rd hour it’ll start again from 00, and for the remaining cases if it is ‘AM’ then the time in 12-hour and 24-hour format will remain same. For example, if the user inputs time as 11:47:56AM the output will be 11:47:56 same as it was in 12-hour format, but if the user enters 12:55:21AM the output will be 00:55:21 because the range of hh’s value is between 00 and 23 both inclusive and immediately after 23rd hour we will be getting 00th hour not 24th.
1. Enter hours 'hh' 09 Enter minutes 'mm' 15 Enter seconds 'ss' 55 Enter string 'am' or 'pm' pm The obtained 24-hour format of input is 21:15:55 2. Enter hours 'hh' 12 Enter minutes 'mm' 00 Enter seconds 'ss' 00 Enter string 'am' or 'pm' am The obtained 24-hour format of input is 00:00:00 3. Enter hours 'hh' 03 Enter minutes 'mm' 55 Enter seconds 'ss' 50 Enter string 'am' or 'pm' am The obtained 24-hour format of input is 03:55:50 4. Enter hours 'hh' 23 Enter minutes 'mm' 13 Enter seconds 'ss' 11 Enter string 'am' or 'pm' am Provide the correct inputs.
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Buy Computer Science Books
- Watch Advanced C Programming Videos
- Buy C Books