Advertisement
nikolay_radkov

LeapYear

Aug 12th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. using System;
  2.  
  3. // Write a program that reads a year from the console and checks whether it is a leap. Use DateTime.
  4.  
  5. class LeapYear
  6. {
  7.     private DateTime date;
  8.  
  9.     // Constructor
  10.     public LeapYear(DateTime date)
  11.     {
  12.         this.date = date;
  13.     }
  14.  
  15.     // Return the current year
  16.     public int Year
  17.     {
  18.         get
  19.         {
  20.             return date.Year;
  21.         }
  22.     }
  23.  
  24.     // Check if the year is leap
  25.     public bool IsLeap()
  26.     {
  27.         bool isLeap = false;
  28.  
  29.         if (this.Year % 4 == 0 && this.Year % 100 != 0  || this.Year % 400 == 0)
  30.         {
  31.             isLeap = true;
  32.         }
  33.  
  34.         return isLeap;
  35.     }
  36.  
  37.     static void Main(string[] args)
  38.     {
  39.         Console.Title = "Leap year";
  40.  
  41.         Console.ForegroundColor = ConsoleColor.Green;
  42.         Console.Write("Enter an year: ");
  43.  
  44.         DateTime date = new DateTime(int.Parse(Console.ReadLine()), 1, 1);
  45.  
  46.         LeapYear leapYear = new LeapYear(date);
  47.  
  48.         Console.ForegroundColor = ConsoleColor.Yellow;
  49.         Console.WriteLine("\nIs the year {0} is leap ?", leapYear.Year); // Calling the property and get the year
  50.         Console.WriteLine("---> Answer: {0} <---", leapYear.IsLeap()); // Calling the method and check the year
  51.  
  52.         // Embedded method to check the result
  53.         Console.ForegroundColor = ConsoleColor.Red;
  54.         Console.WriteLine("\n===> Answer: {0} <=== Embedded method for check", DateTime.IsLeapYear(leapYear.Year));
  55.  
  56.         Console.WriteLine();
  57.         Console.ResetColor();
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement