Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In military time, hours are numbered from 00 to 23. Under this system, midnight

ID: 3876530 • Letter: I

Question

In military time, hours are numbered from 00 to 23. Under this system, midnight is 00, 1 a.m. is 01, 12 p.m. is 12, 1 p. m. is 13, and so on. Time in hours and minutes in given as a four-digit number with minutes following hours. For instance, military time 0022 corresponds to 12:22 a.m. regular time, and military time 1200 corresponds to noon regular time. Write a C program that converts from military time to regular time. An example input/output:

Enter a military time (0000 to 2359): 1634

Output: The regular time is 4:34 pm.

1. %2d as the format specifier for scanf can be used to obtain two digits of a number.

2. If the time entered is not in the range of 0000 to 2359, output an error message and abort the program.

Please explain steps on how you came to the conclusion on what functions to use and your thought process while coding this.

Explanation / Answer

#include <iostream>

using namespace std;

int main() {

int a,b;

  scanf("%2d%2d",&a,&b);

  if(a>23 || b>59)

      printf("Incorrect Input");

  else{

      if(a>11)

          printf("The time is %d:%dpm",a-12,b);

      else

          printf("The time is %d:%dam",a,b);

  }

  return 0;

}

This is a very simple code with a for hours and b being used to store the minutes. If you are unable to understand anything pls comment.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote