Leap Year Program in Java
Now, Here we going to write a java program to check year is leap year or not. This leap year java program is the most important program because it is mostly asked in java programming interviews.
Let's write a java program to check whether the entered year is leap year or not.
Let's write a java program to check whether the entered year is leap year or not.
What is Leap Year?
Normally every year comes with 365 days but when any year comes with 366 days is known as leap year.
Let's understand with an example where we will take year as an input from the user by using Scanner class and then calculate the year is leap year or not.
Let's understand with an example where we will take year as an input from the user by using Scanner class and then calculate the year is leap year or not.
Java Leap Year Program Example
This is simple java program to find leap year or not with line-by-line.
import java.util.Scanner;
class LeapYearExample
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the year");
int year = sc.nextInt();
boolean b = false;
if(year % 400 == 0)
{
b = true;
}
else if(year % 100 == 0)
{
b = false;
}
else if(year % 4 == 0)
{
b = true;
}
else
{
b = false;
}
if(b)
{
System.out.println(" year "+year+" is a leap year ");
}
else
{
System.out.println(" year " +year+" is not a leap year ");
}
}
}
Output : Enter the year
2000
year 2000 is a leap year
Note: If a year is divisible by 400 and 4, year is leap year. If year is divisible by 100, not a leap year.
Check some other basic java programs.
Learn how to convert decimal to binary in java and how to find area of triangle in java.
Here we learned how to calculate leap year in java through program.
Output : Enter the year
2000
year 2000 is a leap year
Note: If a year is divisible by 400 and 4, year is leap year. If year is divisible by 100, not a leap year.
Check some other basic java programs.
Learn how to convert decimal to binary in java and how to find area of triangle in java.
Here we learned how to calculate leap year in java through program.
Nice..
ReplyDeletegreat example of leap year in java
ReplyDelete