Java Programming Examples Tutorial Index

Java Number Programs

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