Validate Leap Year in C# Console

Today in C#, i will teach you how to validate a year as leap year or not. Hence we already know that leap year has 366 days instead of the normal 365 days. Leap years occur every 4 years. And this fact is our formula in finding a leap year using the console application in C#. Now, let's start this tutorial! 1. Let's start with creating a Console Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Console Application. 2. Name your project as LeapYear as your module. Note: This is a console application so we cannot have visual controls for this tutorial. 3. Add the following code in your module. Initialize variable yeear as Integer and make it input as your inputted year.
  1. namespace LeapYear
  2. {
  3.         sealed class LeapYear
  4.         {
  5.                 static public void main()
  6.                 {
  7.                         int yeaar = default(int);
  8.                         Console.Write("Enter Year:");
  9.                         yeaar = int.Parse(Console.ReadLine());
Make an If Else Statement because we all know that leap year has an exception that every 100 years special rules apply and will be a leap year. For example 1900 was not a leap year, but 2000 was. We used the Mod operator to find the remainder of your inputted year divided by 100 that is not equal to 0. Then, after that filter again the year that a leap year will occur in every 4 years because of February 29. Otherwise, it is not a leap year.
  1.                         {
  2.                                 if (yeaar % 4 == 0)
  3.                                 {
  4.                                         Console.WriteLine(yeaar.ToString() + " is a Leap year");
  5.                                 }
  6.                                 else
  7.                                 {
  8.                                         Console.WriteLine(yeaar.ToString() + " is NOT a Leap year");
  9.                                 }
  10.                         }
Next, if 100 multiplied by 4 years and that is 400 it will be a leap year. Otherwise, not. This is an else statement that is not equal to the 100 years that leap years special rule is applied.
  1. else
  2.                         {
  3.                                 if (yeaar % 400 == 0)
  4.                                 {
  5.                                         Console.WriteLine(yeaar.ToString() + " is a Leap year");
  6.                                 }
  7.                                 else
  8.                                 {
  9.                                         Console.WriteLine(yeaar.ToString() + " is NOT a Leap year");
  10.                                 }
  11.                         }
  12.                         Console.ReadLine();
  13.                 }
  14.         }
  15. }
Full source code:
  1. using System.Diagnostics;
  2. using System.Data;
  3. using System.Collections;
  4. using Microsoft.VisualBasic;
  5. using System.Collections.Generic;
  6. using System;
  7.  
  8.  
  9.  
  10. namespace LeapYear
  11. {
  12.         sealed class LeapYear
  13.         {
  14.                 static public void main()
  15.                 {
  16.                         int yeaar = default(int);
  17.                         Console.Write("Enter Year:");
  18.                         yeaar = int.Parse(Console.ReadLine());
  19.                         if (yeaar % 100 != 0)
  20.                         {
  21.                                 if (yeaar % 4 == 0)
  22.                                 {
  23.                                         Console.WriteLine(yeaar.ToString() + " is a Leap year");
  24.                                 }
  25.                                 else
  26.                                 {
  27.                                         Console.WriteLine(yeaar.ToString() + " is NOT a Leap year");
  28.                                 }
  29.                         }
  30.                         else
  31.                         {
  32.                                 if (yeaar % 400 == 0)
  33.                                 {
  34.                                         Console.WriteLine(yeaar.ToString() + " is a Leap year");
  35.                                 }
  36.                                 else
  37.                                 {
  38.                                         Console.WriteLine(yeaar.ToString() + " is NOT a Leap year");
  39.                                 }
  40.                         }
  41.                         Console.ReadLine();
  42.                 }
  43.         }
  44. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon R. Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Visit and like my page on Facebook at: https://www.facebook.com/BermzISware Output: output

Add new comment