Advertisement
touhid_xml

Dicover leapyear in C#

Nov 28th, 2016
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Leap_Year
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Console.WriteLine("Enter Year to Discover Leap Year:");
  13.  
  14.             var year = Console.ReadLine(); //get user input
  15.  
  16.  
  17.             String newDateString = String.Format("05/01/{0}",year.ToString()); //format a complete date
  18.  
  19.             DateTime myDate;
  20.  
  21.             if (DateTime.TryParse(newDateString, out myDate))  //parse date from userinput
  22.             {
  23.                 if (DateTime.IsLeapYear(myDate.Year))
  24.                 {
  25.                     Console.WriteLine(myDate.Year.ToString() + " is leapyear");
  26.                 }
  27.                 else
  28.                 {
  29.                     Console.WriteLine(myDate.Year.ToString() + " is not leapyear");
  30.                 }
  31.             }
  32.             else
  33.             {
  34.                 Console.WriteLine("Invalid year input");
  35.             }
  36.  
  37.             Console.ReadKey(); //pause the program
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement