Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 1. Write a program that reads a year from the console and
- checks whether it is a leap. Use DateTime.
- Напишете програма, която прочита от конзолата
- година и проверява дали е високосна.
- In the Gregorian calendar 3 criteria must be taken into
- account to identify leap years:
- -The year is evenly divisible by 4;
- -If the year can be evenly divided by 100, it is NOT a
- leap year, unless;
- -The year is also evenly divisible by 400. Then it is a
- leap year.
- */
- using System;
- using System.Linq;
- using System.Diagnostics;
- class CheckIfYearIsLeap
- {
- static void Main()
- {
- do
- {
- Stopwatch stopWatch = new Stopwatch();
- // ---------- Begin: -----------------------
- Console.Write("\nPlease, Enter a Year: ");
- int year = int.Parse(Console.ReadLine());
- stopWatch.Start();
- Console.WriteLine("\n1. Difines if the Year is Leap using DateTime.IsLeapYear:\n");
- DateTimeIsLeapYear(year);
- Console.WriteLine("\n2. Difines if the Year is Leap using Leap-Criteria: \n");
- CreteriaDefinitionOfLeap(year);
- // ------- end: -----------------------------
- stopWatch.Stop();
- Console.WriteLine("\n\n\nRuntime: {0}", stopWatch.Elapsed);
- } while (EndMethod() == -1);
- }
- //=============== METHODS: ====================
- // Difines if the Year is Leap using DateTime.IsLeapYear:
- static void DateTimeIsLeapYear(int year)
- {
- if (DateTime.IsLeapYear(year))
- {
- Console.WriteLine("\nThe year is leap.\n");
- }
- else
- {
- Console.WriteLine("\nThe year is not leap.\n");
- }
- }
- // Difines if the Year is Leap by Leap Criteria:
- static void CreteriaDefinitionOfLeap(int year)
- {
- if (year % 100 == 0)
- if (year % 400 == 0)
- Console.WriteLine("\nThe Year is Leap");
- else
- Console.WriteLine("\nThe Year is NOT Leap");
- else if (year % 4 == 0)
- Console.WriteLine("\nThe Year is Leap");
- else
- Console.WriteLine("\nThe Year is NOT Leap");
- }
- // ------------ End Method: ------------------------
- static int EndMethod()
- {
- Console.ForegroundColor = ConsoleColor.DarkRed;
- Console.Write("\nPress Any Key to Exit or press \"c\" to continue");
- Console.ResetColor();
- ConsoleKeyInfo key = Console.ReadKey();
- if (key.KeyChar == 'c')
- {
- Console.Clear();
- return -1;
- }
- else
- {
- Environment.Exit(0);
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement