fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int hour, minute;
  5.  
  6. // Input: Hour and Minute in 24-hour format
  7. printf("Enter time in 24-hour format (HH:MM): ");
  8. scanf("%d:%d", &hour, &minute);
  9.  
  10. // Check for invalid input
  11. if (hour < 0 || hour > 24 || minute < 0 || minute >= 60) {
  12. printf("Invalid time\n");
  13. return 0;
  14. }
  15.  
  16. // Conversion logic
  17. if (hour == 0) { // 00:00 in 24-hour -> 12:00 AM
  18. printf("%02d:%02d AM\n", 12, minute);
  19. } else if (hour == 12) { // 12:00 in 24-hour -> 12:00 PM
  20. printf("%02d:%02d PM\n", 12, minute);
  21. } else if (hour > 12 && hour < 24) { // 13-23 hours -> Subtract 12 and append PM
  22. printf("%02d:%02d PM\n", hour - 12, minute);
  23. } else if (hour < 12) { // 01-11 hours -> AM
  24. printf("%02d:%02d AM\n", hour, minute);
  25. } else if (hour == 24) { // 24:00 is 12:00 PM
  26. printf("%02d:%02d PM\n", 12, minute);
  27. }
  28.  
  29. return 0;
  30. }
Success #stdin #stdout 0s 5276KB
stdin
Standard input is empty
stdout
Enter time in 24-hour format (HH:MM): Invalid time