Programming contests

20 Programming Exercise for Beginners

January 1, 2019 12:00 AM – December 31, 2019 12:00 AM

Time Conversion

Create a program that converts time from 24-hour clock format to 12-hour clock format.

Input Specification

The input is a sequence of lines, each of which containing a time in 24-hour clock form, where hours and minutes are separated by a dot. The minute values are left-padded to two decimal digits with zeros.

Sample Input

  1. 0.02
  2. 11.58
  3. 12.32
  4. 13.29
  5. 22.17
download as text file

Output Specification

For each line of the input, the output contains the same time in 12-hour clock format. The separator is also a dot, and the minute values are also left-padded as it was defined in the input specification. Each time before noon ends with the letters “am”, each time in the afternoon ends with the letters “pm”, and noon is displayed as “12:00pm”, while midnight is displayed as “12.00am”.

Output for Sample Input

  1. 12.02am
  2. 11.58am
  3. 12.32pm
  4. 1.29pm
  5. 10.17pm
download as text file

Hints and Guide

Based on the time in 24-hour clock format, four different cases must be distinguished:

  • if the hour part is 0, then the suffix is „am” and the hour part of the output is 12;
  • if the hour part is between 1 and 11, then the suffix is „am” and the hour part of the output is the same as that of the input;
  • if the hour part is 12, then the suffix is „pm” and the hour part of the output is 12;
  • if the hour part is greater than 12, then the suffix is „pm” and we need to subtract 12 from the hour part of the input.

It is recommended to use the printf function because the format string supports solutions for the padding.

One possible solution is available in: main.c.

Acknowledgement This work was supported by the construction EFOP-3.4.3-16-2016-00021. The project was supported by the European Union, co-financed by the European Social Fund.
University of Debrecen; Faculty of Informatics; v. 03/01/2019