Leap year java program: Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Program To Check Leap Year
Leap year program in java: In this article we will see different way to find out a year is leap year or not
Concept :
Earth takes 365.25 days to roam around the orbit of the Sun i.e., called a solar year. We always round up the days in a calendar year to 365. And we add the 4 partial days to count it to one day , then we add one day to our calendar in every four years. This year having extra one days is called a leap year.
This leap year exactly divisible by 4 , and 400 also . but in the exception the year which is a century year (i.e., ending with 00 ) is not included in the leap year .
Example – 2016 is a leap year .
2000 is a leap year .
But 1700 is not a leap year .
Now we will see one by one approach to check a year is leap year or not.
- Checking a year is leap year or not by if-else ladder
- Checking a year is leap year or not by using logical operator
- Checking a year is leap year or not by using ternary operator
Method-1 : Checking a year is leap year or not by if-else ladder
By using if else ladder we can check a year is leap year or not .
Approach :
- Enter a year .
- Take a Boolean value .
- Using if else ladder , check every possible condition .
- At last, if Boolean value is true then year is a leap year .
- If Boolean value is false, then year is not a leap year .
Let’s see the below program to understand it clearly.
Program:
import java.util.Scanner;
public class Year {
public static void main(String[] args)
{
// initializing the variable for entering the year value
int y;
Scanner sc = new Scanner(System.in);
// entering year, we want to check
System.out.println("Enter any Year:");
y = sc.nextInt();
sc.close();
// initializing a boolean value .
boolean leap = false;
// checking year is devisable by 4 or not
if(y % 4 == 0)
{
// checking year is devisable by 100 or not
if( y % 100 == 0)
{
// checking year is devisable by 400 or not
if ( y % 400 == 0)
leap = true;
else
leap = false;
}
else
leap = true;
}
else {
leap = false;
}
// printing the result .
if(leap ==true)
System.out.println("\n"+ y + " is a Leap Year.");
else
System.out.println("\n"+ y + " is not a Leap Year.");
}
}
Output: Enter any Year: 1700 1700 is not a Leap Year.
Method-2 : Checking a year is leap year or not by using logical operator
By using logical operator, we can check a year is leap year or not.
Approach :
- Enter a year .
- Take a Boolean value .
- In if condition we are using logical operator to check the leap year .
- If condition (y%100 == 0 && y%100 != 0 || y%400 == 0) satisfies then entered year is leap year .
Program:
import java.util.Scanner;
public class Year
{
public static void main(String[] args)
{
// initializing the variable for entering the year value
int y;
Scanner sc = new Scanner(System.in);
// entering year, we want to check
System.out.println("Enter any Year:");
y = sc.nextInt();
sc.close();
// checking leap year or not using logical operator
if(y%100 == 0 && y%100 != 0 || y%400 == 0)
System.out.println("\n"+ y + " is a Leap Year.");
else
System.out.println("\n"+ y + " is not a Leap Year.");
}
}
Output: Enter any Year: 1700 1700 is not a Leap Year.
Method 3 : Checking a year is leap year or not by using ternary operator
By using ternary / comparison operator, we can check a year is leap year or not.
Approach :
- Enter a year .
- Take a Boolean value .
- In if condition we are using logical operator to check the leap year .
- Taking a variable and applying condition (y % 100 == 0) ? ( (y % 400 == 0)? true : false ) : ( y % 4 == 0 ? true : false ) will return a value true or false .
- If returned value is true, then year is a leap year .
Program:
import java.util.Scanner;
public class Year
{
public static void main(String[] args)
{
// initializing the variable for entering the year value
int y ;
boolean temp;
Scanner sc = new Scanner(System.in);
// entering year, we want to check
System.out.println("Enter any Year:");
y = sc.nextInt();
sc.close();
// checking leap year or not using ternary operator
temp = (y % 100 == 0) ? ( (y % 400 == 0)? true : false ) : ( y % 4 == 0 ? true : false );
if( temp==true)
{
System.out.println("\n"+ y + " is a Leap Year.");
}
else
{
System.out.println("\n"+ y + " is not a Leap Year.");
}
}
}
Output: Enter any Year: 1700 1700 is not a Leap Year.
Get started with learning the programming language Java from beginner to experienced level by referring to our collection of Java Programs with Source Code and become a pro in the subject.
Related Java Decision Making and Loop Programs:
- Java Program to Check Whether a Number is Positive or Negative
- Java Program to Check Whether a Character is Alphabet or Not
- Java Program to Calculate the Sum of Natural Numbers
- Java Program to Find Factorial of a Number
- Java Program to Generate Multiplication Table
- Java Program to Find GCD of two Numbers
- Java Program to Find LCM of two Numbers
- Java Program to Display Alphabets (A to Z) using loop
- Java Program to Count Number of Digits in an Integer
- Java Program to Check Palindrome
- Java Program to Check Whether a Number is Prime or Not
- Java Program to Check Armstrong Number
- Java Program to Display Armstrong Number Between Two Intervals
- Java Program to Make a Simple Calculator Using switch…case
- Java Program to Sort Elements in Lexicographical Order (Dictionary Order)