Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    Java Program to Check Leap Year

    This Java program does leap year checks. It takes input from the user in the form of integers and performs simple arithmetic operations within the condition statement for the output.

    Example:

    import java.util.Scanner;
    
    public class LeapYearCheck {
    
      public static void main(String[] args) {
    
        //Variable definition and assignment
        int year = 2016;
        boolean leap = false;
        Scanner obj = new Scanner(System.in); /* create a object */
        
        //Remove comments from the bottom lines to take input from the user
        //System.out.print("Enter a year: ");
        //year = obj.nextInt();
    
        //A year divisible by 4 is a leap year
        if (year % 4 == 0) {
    
          //It is a centenary year if the value is divisible by 100 with no remainder.
          if (year % 100 == 0) {
    
            //Centenary year is a leap year divided by 400
            if (year % 400 == 0)
              leap = true;
            else
              leap = false;
          }
          
          // if the year is not century
          else
            leap = true;
        }
        //The Year is not a leap year
        else
          leap = false;
    
        if (leap)
          System.out.println(year + " is a leap year.");
        else
          System.out.println(year + " is not a leap year.");
      }
    }

    Program Output:

    Enter a year: 2016
    2016 is a leap year.


    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.