You are on page 1of 2

Leap Year indicator

using System;

class Program
{
static void Main()
{
Console.Write("Enter a year: ");
int year = int.Parse(Console.ReadLine());

bool isLeapYear = false;

if (year % 4 == 0)
{
if (year % 100 != 0 || year % 400 == 0)
{
isLeapYear = true;
}
}

if (isLeapYear)
{
Console.WriteLine("The year is a leap year.");
}
else
{
Console.WriteLine("The year is not a leap year.");
}
}
}

In this program, we prompt the user to enter a year using Console.Write() . We read the
input as a string using Console.ReadLine() and then convert it to an int using
int.Parse() so that we can perform the leap year calculation.
To determine if a year is a leap year, we apply the following rules:

If the year is divisible by 4, it could be a leap year.

If the year is divisible by 100, it is not a leap year unless it is also divisible by 400.

We use nested if statements to check these conditions. If the year satisfies these
conditions, we set the isLeapYear variable to true . Finally, we use an if statement to
check the value of isLeapYear and display the appropriate message using
Console.WriteLine() .

Additional Explanations:
: This line reads a line of text input from the
int year = int.Parse(Console.ReadLine());

console using Console.ReadLine() , converts it to an integer using int.Parse , and assigns


the result to the year variable. This line effectively reads the user's input as a year.

Leap Year indicator 1


bool isLeapYear = false; : This line declares a boolean variable named isLeapYear and
initializes it with the value false . This variable will be used to store whether the input
year is a leap year or not.

if (year % 4 == 0) : This line begins an if statement, which checks if the year is


divisible by 4 without a remainder. If this condition is true, it means the year is a
potential leap year.

if (year % 100 != 0 || year % 400 == 0) : This line is nested inside the previous if

statement. It checks two conditions:

year % 100 != 0 : This checks that the year is not divisible by 100. If it's not divisible by
100, it's a potential leap year.

year % 400 == 0

: This checks that the year is divisible by 400. If it is, it's also a potential leap year.
If either of these conditions is true, it sets isLeapYear to true .

The following if and else blocks use the value of isLeapYear to print whether the year
is a leap year or not to the console.

Leap Year indicator 2

You might also like