Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Java Leap Year Program

by suresh

How to write Java Leap Year Program using If Statement, Nested If Statement, and Else If Statement in Java Programming with example?. Before we get into Java Leap Year Program, Let us see the logic and definition behind the Leap Year

Java Leap year

The normal year contains 365 days, but the leap year contains 366 days. This extra day added to the February month, that is why we get February 29. As per Mathematics, except century years, Years perfectly divisible by four are called Leap years. Century year’s means they end with 00 such as 1200, 1300, 2400, 2500 etc (Obviously they are divisible by 100). For these century years, we have to calculate further to check the Leap year.

  • If the century year is divisible by 400, then that year is a Leap year
  • If the century year is not divisible by 400, then that year is not a Leap year.

Java Leap Year Program using If Statement

This Java program for leap year allows the user to enter any year. Then this Java program will check whether the user entered year is Leap year or not using the If Else statement

// Java Leap Year Program using If Statement

package DatePrograms;

import java.util.Scanner;

public class LeapYearUsingIf {
	private static Scanner sc;

	public static void main(String[] args) {
		int year;
		sc = new Scanner(System.in);
		
		System.out.println("\n Please Enter any year you wish: ");
		year = sc.nextInt();
		
		if (( year%400 == 0)|| (( year%4 == 0 ) &&( year%100 != 0))) {
			System.out.format("\n %d is a Leap Year. \n", year);
		}
		else {
			System.out.format("\n %d is NOT a Leap Year. \n", year);
		}
	}
}
Java Leap Year Program using If Statement

Let us check for a normal year (Not Leap Year)

Java Leap Year Program using If Statement 1

In this Java leap year program, Since we have to check multiple conditions within one If Statement, we used Logical AND and Logical OR operators. Let us divide the condition to understand it better

1: ( year % 400 == 0)||

2: ( year % 4 == 0 ) &&

3: ( year % 100 != 0))

The first condition (year%400 == 0) will check whether the (year%400) reminder is precisely equal to 0 or not. As per the algorithm, any number that is divisible by 400 is a Leap year.

OR

The second Java If Else statement condition holds two statements, so they both have to be TRUE.

  • The first condition (year%4 == 0) will check whether the remainder of the (year % 4) is exactly equal to 0 or not. If the condition is False, then it will exit from the condition because there is no point in checking the other condition. It is not the Leap year. AND
  • The second condition will check (year % 100) reminder is not equal to 0. If it is TRUE, then the given number is not a century number.
  • Any number that is divisible by four but not by 100, then that number is Leap Year.

TIP: Please refer to Java Logical Operators to understand the Logical And and Logical Or in Java.

Java Leap Year Program using Else If Statement

This Java leap year program allows the user to enter any year. Then the Java program will check whether the user entered year is Leap year or not using the Java Else If statement.

// Java Leap Year Program using Else If Statement

package DatePrograms;

import java.util.Scanner;

public class LeapYearUsingElseIf {
	private static Scanner sc;

	public static void main(String[] args) {
		int year;
		sc = new Scanner(System.in);
		
		System.out.println("\n Please Enter any year you wish: ");
		year = sc.nextInt();
		
		if ( year % 400 == 0) {
			System.out.format("\n %d is a Leap Year. \n", year);
		}
		else if (year%100 == 0) {
			System.out.format("\n %d is NOT a Leap Year. \n", year);
		}
		else if(year%4 == 0) {
			System.out.format("\n %d is a Leap Year. \n", year);
		}
		else {
			System.out.format("\n %d is NOT a Leap Year. \n", year);
		}
	}
}
Java Leap Year Program using Else If Statement

In this Java leap year program, the following statements ask the user to enter any year to check whether that year is Leap year or Not. Next, we are going to assign the user entered value to year variable

System.out.println("\n Please Enter any year you wish: ");
year = sc.nextInt();
  • The first If condition will check whether the (year % 400) reminder is exactly equal to 0. As per the algorithm, any number that is divisible by 400 is a Leap year. If this condition Fails, then it will go to the next condition.
  • The second If condition will check (year % 100) reminder is exactly equal to 0 or not. As per the algorithm, any number that is not divisible by 400 but by 100 is Not a Leap year (Century Year). We checked (year % 400) in the First If statement. Because it failed, so it came to the second condition. If both the first and second condition Fails, then it will go to the third condition.
  • The third condition will check whether year mod 4 is equal to 0. If this condition is True, then given year is Leap year because We already checked for the century years in the previous condition. If all the statements Fail, then it will go the Else statement at the end.
  • If all the above statements fail, then it is not a Leap year in Java.

Java Leap Year Program using Nested If Statement

This leap year program in Java helps the user to enter any year. Then the Java program will check whether the user entered year is Leap year or not using the Nested If in Java

// Java Leap Year Program using Nested If Statement
package DatePrograms;

import java.util.Scanner;

public class LeapYearUsingNestedIf {
	private static Scanner sc;

	public static void main(String[] args) {
		int year;
		sc = new Scanner(System.in);
		
		System.out.println("\n Please Enter any year you wish: ");
		year = sc.nextInt();
		
		if ( year % 4 == 0) {
			if (year%100 == 0) {
				if(year%400 == 0) {
					System.out.format("\n %d is a Leap Year. \n", year);
				}
				else {
					System.out.format("\n %d is NOT a Leap Year. \n", year);
				}
			}
			else {
				System.out.format("\n %d is a Leap Year. \n", year);
			}
		}
		else {
			System.out.format("\n %d is NOT a Leap Year. \n", year);
		}
	}
}
Java Leap Year Program using Nested If Statement

In this Java leap year program, the User will enter any year to check whether that year is Leap year or Not. The first If condition will check whether the remainder of the (year%4) is precisely equal to 0 or not.

  • If the condition is False, then the given number is not the Leap year.
  • If the condition is True, then we have check further for the century year. So the Javac compiler will go to the Nested If condition.

The second If condition will check (year%100) reminder is exactly equal to 0 or Not.

  • If this condition is False, then the year is not a century year. So the given number is a Leap year.
  • If the condition is True, then we have check whether the number is divisible by 400 or not. So the Javac compiler will goto to another Nested If condition.

In this condition, the Javac will check whether the remainder of the (year%400) is exactly equal to 0 or not.

  • If the condition is False, then the given number is not the Leap year.
  • If the condition is True, then the given number is Leap Year in java

Java Leap Year Program using Oops

This program for java leap year allows entering any positive integer (year). Then the Java program checks whether the given year is a leap year or not. In this example, we are dividing the code using the Object-Oriented Programming.

To do this, first, we will create a class that holds a method to reverse an integer recursively.

package DatePrograms;

public class LeapYear {
	public int CheckLeapYear(int year) {
		if (( year%400 == 0)|| (( year%4 == 0 ) && ( year%100 != 0))) {
			return year;
		}
		else {
			return 0;
		}
	}
}

Within the Main program of the leap year program in java, we will create an instance of the above-specified class and call the methods.

package DatePrograms;
import java.util.Scanner;
public class LeapYearUsingClass {
	private static Scanner sc;
	public static void main(String[] args) {
		int year, leap;
		sc = new Scanner(System.in);
		System.out.println(" Please Enter any year you wish: ");
		year = sc.nextInt();
		
		LeapYear ly = new LeapYear();
		leap = ly.CheckLeapYear(year);
		
		if(leap != 0) {
			System.out.format("\n %d is a Leap Year. \n", year);
		}
		else {
			System.out.format("\n %d is NOT a Leap Year. \n", year);
		}
	}
}
Java Leap Year Program using OOPS

LeapYear Class Analysis:

In this java leap year program, first, we declared an integer function CheckLeapYear with one argument. Within the function, we used the If statement to check whether the given year is a leap year or Not and if it is true, then it will return year. Otherwise, it returns zero. We already explained the Logic in the above example.

Main Class Analysis:

First, we created an instance / created an Object of the LeapYear Class

LeapYear ly = new LeapYear();

Next, we are calling the CheckLeapYear method.

leap = ly.CheckLeapYear(year);

Lastly, System.out.println statement will print the output.

Placed Under: Java Programs

  • Java Hello World Program
  • Java Add Two Numbers Program
  • Java Compound Interest Program
  • Java Number Divisible by 5 & 11
  • Java Cube of a Number Program
  • Java Print Even Numbers 1 to N
  • Java GCD of Two Numbers
  • Java LCM of Two Numbers
  • Java Largest of Two Numbers
  • Java Largest of Three Numbers
  • Java Print Multiplication Table
  • Java Odd Numbers from 1 to N
  • Java Odd Even Program
  • Java find +Ve or -Ve number
  • Java Power of a Number Program
  • Java Calculate Profit or Loss
  • Java Print 1 to 100 without Loop
  • Java Quadratic Equation roots
  • Java Square of Number Program
  • Java Simple Interest Program
  • Java Sum of Even Numbers
  • Java Sum of Odd numbers
  • Java Sum of Even & Odd Number
  • Java find Total ,Average & Percentage of 5 Subjects
  • Java ASCII Value of a Character
  • Java ASCII Value of all Characters
  • Java String Comparison program
  • Java Area of Circle Program
  • Java Area Of Triangle Program
  • Java Area Of Trapezoid Program
  • Java Area of Equilateral Triangle
  • Java Area of Rectangle Program
  • Java Area of Right angle triangle
  • Java Volume & Surface of Sphere
  • Java Volume & Surface of Cone
  • Java Volume & Surface of Cuboid
  • Java Volume & Surface of Cube
  • Java Cylinder Volume & Surface
  • Java Math Library
  • Java Armstrong Number Program
  • Java Perfect Number Program
  • Java Palindrome Program
  • Java Count Digits in a Number
  • Java Calculate Electricity Bill
  • Java Find Factors of a Number
  • Java Factorial Program
  • Java Fibonacci Series program
  • Java find First Digit of a Number
  • Java find Last Digit of a Number
  • Java First & Last Digit of Number
  • Java Generic Root of Number
  • Java Natural Numbers in Reverse
  • Java Natural Numbers from 1 to N
  • Java Find Sum of Prime Numbers
  • Java Print Prime Numbers 1 to N
  • Java Prime Number Program
  • Java Reverse a Number Program
  • Java Strong Number Program
  • Java Sum of N Natural Numbers
  • Java Sum of Digits Program
  • Java Swap Two Numbers
  • Java Leap Year Program
  • Java Number of Days in a Month
  • Java Array
  • Java Array Arithmetic Operations
  • Java Program to Copy an Array
  • Java Count Array Duplicates
  • Java count even number in array
  • Java count odd numbers in Array
  • Java Count Even & Odds in Array
  • Java Count Negative Array Items
  • Java Count Positive Array Items
  • Java Count Array +Ve & -Ve num
  • Java Delete Array Duplicates
  • Java Large & Small Array Item
  • Java Largest Array Number
  • Java Print Array Elements
  • Java Print Positive Array Items
  • Java Print Negative Array Items
  • Java Smallest Array Number
  • Java Second Largest Array Item
  • Java Print Unique Array Items
  • Java +Ve & -Ve in separate array
  • Java Sort Array in Descending
  • Java Sort Array in Ascending
  • Java Sum of Array Elements
  • Java Sum of Array Even Numbers
  • Java Sum of Array Odd Numbers
  • Java Sum of Array Even and Odd
  • Java Swap Arrays without temp
  • Java Matrix Introduction
  • Java Print Matrix items
  • Java Check 2 Matrices are Equal
  • Java Matrix Arithmetic operation
  • Java Matrix Addition
  • Java Matrix Multiplication
  • Java Find Matrix Determinant
  • Java Identity Matrix
  • Java Interchange Matrix diagonal
  • Java Matrix Lower Triangle
  • Java Matrix Lower Triangle Sum
  • Java Matrix Upper Triangle
  • Java Matrix Upper Triangle Sum
  • Java Scalar Matrix Multiplication
  • Java Sparse Matrix
  • Java Sum of Matrix Diagonal
  • Java Sum of Matrix Opp Diagonal
  • Java Sum of each Matrix Row
  • Java Sum of each Matrix Column
  • Java Sum of Matrix row & column
  • Java Symmetric Matrix
  • Java Transpose Matrix
  • Java Rectangle Star Pattern
  • Java Square Star Pattern
  • Java Hollow Box Number Pattern
  • Java Box Number Pattern of 1, 0
  • Java Square Number Pattern
  • Java Print Floyd’s Triangle
  • Java Print String Characters
  • Java String replace 1st Char Occ
  • Java String replace last Char occ
  • Java String remove 1st Char Occ
  • Java string remove last Char Occ
  • Java String remove 1st, last char
  • Java String remove all char occur
  • Java String Find 1st Char Occur
  • Java String find all char occur
  • Java String Find Last Char Occur
  • Java String Print 1st & last Char
  • Java String Chars ASCII values
  • Java Program to Concat Strings
  • Java String Characters count
  • Java Count Total Char Occur
  • Java Max Occur String Character
  • Java Toggle String Characters
  • Java Frequency of each Char
  • Java Count Vowels & Consonants
  • Java Count alph digits, special
  • Java Palindrome String
  • Java Program to Reverse a String
  • Java Min Occur String character
  • Java Convert Lower to Upper
  • Java Convert Upper to Lower
  • Java Count total String words

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy