How to check year is leap year or not in C#

How to check year is leap year or not in C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace value
{
    class Program
    {
        static void Main(string[] args)
        {
            int year;
            Console.WriteLine("Enter the year");
            year = int.Parse(Console.ReadLine());
            if (year % 4 == 0)
            {
                if (year % 100 == 0)
                {
                    if (year % 400 == 0)
                    {
                        Console.WriteLine(year + " is leap year");
                    }
                    else
                    {
                        Console.WriteLine(year + " is not leap year");
                    }

                }
                else
                {
                    Console.WriteLine(year + " is leap year");
                }                  
            }
            else
            {
                Console.WriteLine(year+" is not leap year");
            }
            Console.ReadLine();
        }
    }
}

output:-







---------------------------------------------------------------------

OR 
using System;

namespace Test_Application
{
    class Program
    {
        static void Main(string[] args)
        {
            int y;

            Console.WriteLine("Enter the year :");
            y = int.Parse(Console.ReadLine());

            if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
            {
                Console.WriteLine("Is Leap year {0}", y);
            }
            else
            {
                Console.WriteLine("Is not Leap year {0}", y);
            }
            Console.ReadLine();
        }
    }
}

Share this

Previous
Next Post »