JAVA program to check if the given year is a leap year or not
This JAVA program checks if the given year is a leap year or not.
Leap year is one which is divisible by 4 and not divisible by 100.
Logic
Will be understood clearly by dry run of the program.
Dry Run of the Program
Take input as year Let us say we took 2000 as year.
As year(200) is divisible by 4 and divisible by 100 then it does not satisfy the IF condition if((year(2000)%4==0 && year(2000)%100!=0) which is false || (year(2000)%4==0 && year(2000)%400==0)) which is true as year(2000) is divisble by 4 and also divisible by 400.
We have used an ||(OR) operator means if any of the condition is satisfied then output the result as true.
Hence the entered year is a leap year.
Program
import java.util.*; 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) || (year%4==0 && year%400==0)) { System.out.println(year+" is a leap year"); } else { System.out.println(year+" is not a leap year"); } } }