Advertisement
vovanhik_24

Task 5

Apr 8th, 2025
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.89 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Task5
  4. {
  5.     public class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             ConvertTo12HourFormat(14, 30);
  10.             ConvertTo12HourFormat(0, 45);
  11.             ConvertTo12HourFormat(12, 0);
  12.             ConvertTo12HourFormat(23, 59);
  13.             ConvertTo12HourFormat(1, 5);
  14.         }
  15.  
  16.         static void ConvertTo12HourFormat(int hours, int minutes)
  17.         {
  18.             if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59)
  19.             {
  20.                 Console.WriteLine("Некорректное время.");
  21.                 return;
  22.             }
  23.  
  24.             string period = hours < 12 ? "AM" : "PM";
  25.             int hour12 = hours % 12;
  26.  
  27.             if (hour12 == 0)
  28.             {
  29.                 hour12 = 12;
  30.             }
  31.  
  32.             Console.WriteLine($"{hour12}:{minutes:D2} {period}");
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement