How to check leap year using java and C#
Hi there, it's quite complicated to check using the if-else statement the year is a leap year or not. Today I will show the example of how to check if the year is a leap year or not.
Leap Year
The example is pretty easy to follow.
Leap Year
- A Normal day for a year is 365 days
- Leap year have 366 days (29 days in February)
- A leap year can be any year that can be divided by 4 without any balance.
- This is because according to research, The earth rotates about 365.242375
- but in one year only have 365 days.
- So something has to be done to handle the remaining 0.242375 balance. that is why the leap year occurs only once in 4 years.
The example is pretty easy to follow.
The Java Application
package leapyear;
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
String Cont = "Y";
Scanner input = new Scanner(System.in);
LeapYear lYear = new LeapYear();
System.out.println("---------------------------");
System.out.println("Program to check if year is leap year or not");
System.out.println("---------------------------");
while(Cont.equalsIgnoreCase("Y")){
System.out.println("Please Enter the year to check in format \"YYYY\"");
String year = "";
while(year.length() != 4){
year = input.nextLine();
if(year.length() != 4){
System.out.println("Please Enter the year in format \"YYYY\"");
}
}
System.out.println("Year " + year + " is a leap year : " + lYear.checkIfYearIsLeapYear(Integer.parseInt(year)));
System.out.println("Do you want to check abother year?(Y,N)");
Cont = input.nextLine();
}
}
public boolean checkIfYearIsLeapYear(int iYear){
if ((iYear % 400 == 0) || ((iYear % 4 == 0) && (iYear % 100 != 0))) {
return true;
} else {
return false;
}
}
}
The C# Program
using System;
using System.Text;
namespace Check_LeapYear
{
class Program
{
static void Main(string[] args)
{
String Cont = "Y";
Program lYear = new Program();
Console.WriteLine("---------------------------");
Console.WriteLine("Program to check if year is leap year or not");
Console.WriteLine("---------------------------");
while (Cont.ToUpper().Equals("Y"))
{
Console.WriteLine("Please Enter the year to check in format \"YYYY\"");
String year = "";
while (year.Length != 4)
{
year = Console.ReadLine();
if (year.Length != 4)
{
Console.WriteLine("Please Enter the year in format \"YYYY\"");
}
}
Console.WriteLine("Year " + year + " is a leap year : " + lYear.checkIfYearIsLeapYear(Convert.ToInt32(year)));
Console.WriteLine("Do you want to check abother year?(Y,N)");
Cont = Console.ReadLine();
}
}
public bool checkIfYearIsLeapYear(int iYear)
{
if ((iYear % 400 == 0) || ((iYear % 4 == 0) && (iYear % 100 != 0)))
{
return true;
}
else
{
return false;
}
}
}
}
The Output
---------------------------
Program to check if year is leap year or not
---------------------------
Please Enter the year to check in format "YYYY"
1989
Year 1989 is a leap year : false
Do you want to check abother year?(Y,N)
Y
Please Enter the year to check in format "YYYY"
89
Please Enter the year in format "YYYY"
2011
Year 2011 is a leap year : false
Do you want to check abother year?(Y,N)
Y
Please Enter the year to check in format "YYYY"
1996
Year 1996 is a leap year : true
Do you want to check abother year?(Y,N)
N