Professional Documents
Culture Documents
Leap Year Indicator Explanations
Leap Year Indicator Explanations
using System;
class Program
{
static void Main()
{
Console.Write("Enter a year: ");
int year = int.Parse(Console.ReadLine());
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 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());
if (year % 100 != 0 || year % 400 == 0) : This line is nested inside the previous if
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.