The Program in C# Program to find If Given year is Leap year or not is given below:
using System;
namespace LeapYearCheck
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Family,enter a year to check if it is a leap year or not!: ");
int year = Convert.ToInt32(Console.ReadLine());
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
Console.WriteLine(year + " is a leap year.");
}
else
{
Console.WriteLine(year + " is not a leap year.");
}
Console.ReadLine();
}
}
}
Output:
Hello Codeauri Family,enter a year to check if it is a leap year or not!:
2020 is a leap year.
Pro-Tips💡
Here are the step by step execution of above program:
- The first line “using System;” specifies that the System namespace is being used in this code, which provides access to various classes and methods including Console for input/output.
- The code defines a namespace named “LeapYearCheck”.
- Inside the namespace, there is a class named “Program” that contains the main entry point of the code.
- The Main method of the “Program” class is where the program’s logic starts.
- The program prompts the user to enter a year using the Console.WriteLine method.
- The year entered by the user is stored in the “year” variable after being converted from a string to an integer using the Convert.ToInt32 method.
- The code then checks if the year is a leap year using an if-else statement. A leap year is a year that is divisible by 4 but not by 100, or a year that is divisible by 400.
- If the year is a leap year, the program outputs a message to the console indicating that it is a leap year. If not, it outputs a message indicating that it is not a leap year.
- The Console.ReadLine method is called at the end of the Main method to pause the console window and wait for the user to press the Enter key before closing.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.