Advertisement
Opteronic

cs 0 Check If Year Is Leap - 2 Methods

Dec 22nd, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. /*
  2.     1. Write a program that reads a year from the console and
  3.     checks whether it is a leap. Use DateTime.
  4.     Напишете програма, която прочита от конзолата
  5.     година и проверява дали е високосна.
  6.  
  7.      In the Gregorian calendar 3 criteria must be taken into
  8.     account to identify leap years:
  9.     -The year is evenly divisible by 4;
  10.     -If the year can be evenly divided by 100, it is NOT a
  11.     leap year, unless;
  12.     -The year is also evenly divisible by 400. Then it is a
  13.     leap year.
  14. */
  15.  
  16. using System;
  17. using System.Linq;
  18. using System.Diagnostics;
  19.  
  20. class CheckIfYearIsLeap
  21. {
  22.     static void Main()
  23.     {
  24.         do
  25.         {
  26.             Stopwatch stopWatch = new Stopwatch();
  27.             //  ----------  Begin:  -----------------------
  28.  
  29.             Console.Write("\nPlease, Enter a Year: ");
  30.             int year = int.Parse(Console.ReadLine());
  31.             stopWatch.Start();
  32.  
  33.             Console.WriteLine("\n1. Difines if the Year is Leap using DateTime.IsLeapYear:\n");
  34.             DateTimeIsLeapYear(year);
  35.  
  36.             Console.WriteLine("\n2. Difines if the Year is Leap using Leap-Criteria: \n");
  37.             CreteriaDefinitionOfLeap(year);
  38.  
  39.             // -------   end: -----------------------------
  40.             stopWatch.Stop();
  41.             Console.WriteLine("\n\n\nRuntime: {0}", stopWatch.Elapsed);
  42.         } while (EndMethod() == -1);
  43.     }
  44.     //=============== METHODS: ====================
  45.  
  46.     // Difines if the Year is Leap using DateTime.IsLeapYear:
  47.     static void DateTimeIsLeapYear(int year)
  48.     {
  49.         if (DateTime.IsLeapYear(year))
  50.         {
  51.             Console.WriteLine("\nThe year is leap.\n");
  52.         }
  53.         else
  54.         {
  55.             Console.WriteLine("\nThe year is not leap.\n");
  56.         }
  57.     }
  58.  
  59.     // Difines if the Year is Leap by Leap Criteria:
  60.     static void CreteriaDefinitionOfLeap(int year)
  61.     {
  62.         if (year % 100 == 0)
  63.             if (year % 400 == 0)
  64.                 Console.WriteLine("\nThe Year is Leap");
  65.             else
  66.                 Console.WriteLine("\nThe Year is NOT Leap");
  67.         else if (year % 4 == 0)
  68.             Console.WriteLine("\nThe Year is Leap");
  69.         else
  70.             Console.WriteLine("\nThe Year is NOT Leap");
  71.     }
  72.  
  73.     // ------------ End Method: ------------------------
  74.     static int EndMethod()
  75.     {
  76.         Console.ForegroundColor = ConsoleColor.DarkRed;
  77.         Console.Write("\nPress Any Key to Exit or press \"c\" to continue");
  78.         Console.ResetColor();
  79.         ConsoleKeyInfo key = Console.ReadKey();
  80.         if (key.KeyChar == 'c')
  81.         {
  82.             Console.Clear();
  83.             return -1;
  84.         }
  85.         else
  86.         {
  87.             Environment.Exit(0);
  88.             return 0;
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement