What is a leap year?
The year is a leap year if it is divisible by 400 or divisible by 4 but not divisible by 100.
Steps to check leap year in java:-
- Take input from the user using a scanner.
- check if the given year is divisible by 400, if this condition is true then the given year is a leap year.
- check else if the year is divisible by 4 but not divisible by 100, if this condition is true then the given year is a leap year.
- else given year is not a leap year.
Leap year program in java:-
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the year");
int year= sc.nextInt();
if(year%4==0 && year%100 !=0) {
System.out.println("Yes");}
else if(year%400==0){
System.out.println("Yes");}
else {
System.out.println("Not");}
}}
Similar Java Tutorials
- Odd Even in Java
- Method to print exception information in Java
- Exception Handling by Using try-catch in Java
- Checked Exception vs Unchecked Exception
- Exception Hierarchy in Java
- Java Exception Handling Interview Questions
- final finally finalize in java
- User Defined Exception in Java
- Exception Handling Keyword in Java
- throw throws in Java
- try with multiple catch block in Java
- Threads in Java
- Thread priority in Java
1 thought on “Leap Year Program in java”