C# Program to check whether entered year is Leap year or not
Leap year is the year occurring once every four years, having 366 days including 29 February. Below is the program which will determine whether the year entered by user is Leap Year of Not.
using System;
namespace Leap_Year
{
class Program
{
static void Main(string[] args)
{
int year;
Console.WriteLine("Please enter the Year to check for Leap Year in four digits");
year = Convert.ToInt32(Console.ReadLine());
if ((year % 4 == 0 && 100 != 0) || (year % 100 == 0))
{
Console.WriteLine("Entered Year {0} is the Leap Year", year);
}
else
{
Console.WriteLine("Entered Year {0} is not the Leap Year", year);
}
Console.ReadLine();
}
}
}
namespace Leap_Year
{
class Program
{
static void Main(string[] args)
{
int year;
Console.WriteLine("Please enter the Year to check for Leap Year in four digits");
year = Convert.ToInt32(Console.ReadLine());
if ((year % 4 == 0 && 100 != 0) || (year % 100 == 0))
{
Console.WriteLine("Entered Year {0} is the Leap Year", year);
}
else
{
Console.WriteLine("Entered Year {0} is not the Leap Year", year);
}
Console.ReadLine();
}
}
}
Output
Please enter the Year to check for Leap Year in four digits
2018
Entered Year 2018 is not the Leap Year
2018
Entered Year 2018 is not the Leap Year
Please enter the Year to check for Leap Year in four digits
2000
Entered Year 2000 is not the Leap Year
2000
Entered Year 2000 is not the Leap Year
Explanation
We can write the same program in a different way. Look at the below program where we have used classes and objects and methods.
using System;
namespace Leap_Year
{
public class Leap_Calculator_class
{
int year;
public void getUserInput()
{
Console.WriteLine("Please enter the Year to check for Leap Year in four digits");
year = Convert.ToInt32(Console.ReadLine());
}
public void calculateLeapYear()
{
if ((year % 4 == 0 && 100 != 0) || (year % 100 == 0))
{
Console.WriteLine("Entered Year {0} is the Leap Year", year);
}
else
{
Console.WriteLine("Entered Year {0} is not the Leap Year", year);
}
Console.ReadLine();
}
}
}
using System;
namespace Leap_Year
{
class Program
{
static void Main(string[] args)
{
Leap_Calculator_class ly = new Leap_Calculator_class();
ly.getUserInput();
ly.calculateLeapYear();
}
}
}
namespace Leap_Year
{
public class Leap_Calculator_class
{
int year;
public void getUserInput()
{
Console.WriteLine("Please enter the Year to check for Leap Year in four digits");
year = Convert.ToInt32(Console.ReadLine());
}
public void calculateLeapYear()
{
if ((year % 4 == 0 && 100 != 0) || (year % 100 == 0))
{
Console.WriteLine("Entered Year {0} is the Leap Year", year);
}
else
{
Console.WriteLine("Entered Year {0} is not the Leap Year", year);
}
Console.ReadLine();
}
}
}
using System;
namespace Leap_Year
{
class Program
{
static void Main(string[] args)
{
Leap_Calculator_class ly = new Leap_Calculator_class();
ly.getUserInput();
ly.calculateLeapYear();
}
}
}
Output
Please enter the Year to check for Leap Year in four digits
2018
Entered Year 2018 is not the Leap Year
2018
Entered Year 2018 is not the Leap Year
Please enter the Year to check for Leap Year in four digits
2000
Entered Year 2000 is not the Leap Year
2000
Entered Year 2000 is not the Leap Year
Explanation
C# Program to check whether entered year is Leap year or not
Reviewed by LanguageExpert
on
February 17, 2018
Rating:
No comments