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.

advertisement
advertisement

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

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.