Validate Leap Year in C# Console
Submitted by donbermoy on Friday, July 11, 2014 - 12:55.
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.
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.
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.
Full source code:
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:
- namespace LeapYear
- {
- sealed class LeapYear
- {
- static public void main()
- {
- int yeaar = default(int);
- Console.Write("Enter Year:");
- yeaar = int.Parse(Console.ReadLine());
- {
- if (yeaar % 4 == 0)
- {
- Console.WriteLine(yeaar.ToString() + " is a Leap year");
- }
- else
- {
- Console.WriteLine(yeaar.ToString() + " is NOT a Leap year");
- }
- }
- else
- {
- if (yeaar % 400 == 0)
- {
- Console.WriteLine(yeaar.ToString() + " is a Leap year");
- }
- else
- {
- Console.WriteLine(yeaar.ToString() + " is NOT a Leap year");
- }
- }
- Console.ReadLine();
- }
- }
- }
- using System.Diagnostics;
- using System.Data;
- using System.Collections;
- using Microsoft.VisualBasic;
- using System.Collections.Generic;
- using System;
- namespace LeapYear
- {
- sealed class LeapYear
- {
- static public void main()
- {
- int yeaar = default(int);
- Console.Write("Enter Year:");
- yeaar = int.Parse(Console.ReadLine());
- if (yeaar % 100 != 0)
- {
- if (yeaar % 4 == 0)
- {
- Console.WriteLine(yeaar.ToString() + " is a Leap year");
- }
- else
- {
- Console.WriteLine(yeaar.ToString() + " is NOT a Leap year");
- }
- }
- else
- {
- if (yeaar % 400 == 0)
- {
- Console.WriteLine(yeaar.ToString() + " is a Leap year");
- }
- else
- {
- Console.WriteLine(yeaar.ToString() + " is NOT a Leap year");
- }
- }
- Console.ReadLine();
- }
- }
- }
Add new comment
- 90 views