C# Program to Check if a Given Year is a Leap Year
Posted by Superadmin on August 11 2022 08:43:43

C# Program to Check if a Given Year is a Leap Year

 

This is a C# Program to check whether the entered year is a leap year or not.

Problem Description

This C# Program Checks Whether the Entered Year is a Leap Year or Not.

Problem Solution

When A year is divided by 4. If the remainder becomes 0 then the year is called a leap year.

Program/Source Code

Here is source code of the C# Program to Check Whether the Entered Year is a Leap Year or Not. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Check Whether the Entered Year is a Leap Year or Not
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Program
{
    class leapyear
    {
        static void Main(string[] args)
        {
            leapyear obj = new leapyear();
            obj.readdata();
            obj.leap();
        }
        int y;
        public void readdata()
        {
            Console.WriteLine("Enter the Year in Four Digits : ");
            y = Convert.ToInt32(Console.ReadLine());
        }
        public void leap()
        {
            if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
            {
                Console.WriteLine("{0} is a Leap Year", y);
            }
            else
            {
                Console.WriteLine("{0} is not a Leap Year", y);
            }
            Console.ReadLine();
        }
    }
}
Program Explanation

In this C# program, we are reading the value of year using ‘year’ variable. When A year is divided by 4. If the remainder becomes 0, then the year is called a leap year.

 

The Nested-If else condition statement is used to check the given year is leap year or not. In if condition statement the modulus of the value of ‘year’ variable by 4 is equal to 0, and the modulus of the value of ‘year’ variable by 100 is not equal to 0 using logical AND operators.

Otherwise, if the condition is false, then execute the else if condition statement. Compute the modulus of the value of ‘year’ variable by 400 is equal to 0 using logical OR operators. If the condition is true then print the statement as leap year. Otherwise, if the condition is false then execute the else statement. Print the statement as not leap year.

Runtime Test Cases
 
Enter the Year in Four Digits : 1004
1004 is a Leap Year