Java Program To Check Leap Year
A Leap Year is a year having 366 days. A year is said to be a leap year if the following conditions are satisfied:
- The year is a multiple of 400.
- The year is multiple of 4 but not 100.
Here, we are given a year and our task is to check whether the given year is a leap year or not. For example,
Input: 2019
Output: Not a leap year
Program 1: Check Leap Year
In this method, we will directly check whether a year is a leap year or not in the main method itself.
Algorithm
- Start
- Declare a variable let's say a year.
- Initialize it.
- Check for the conditions.
- If the condition satisfies then it is a leap year else not.
- Display the result.
- Stop.
Below is the code for the same.
In the below program, we check whether the given year is a leap year or not in the main method itself.
//Java Program to check whether the given year is a leap year or not
import java.util.Scanner;
public class CheckYear
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int year; //Year Declaration
System.out.println("Enter the year");
year=sc.nextInt(); //Year Initialization
//Check for leap year
if(((year % 4 == 0) && (year % 100 != 0)) ||
(year % 400 == 0))
System.out.println(year+" is a leap year");
else
System.out.println(year+" is not a leap year");
}
}
Enter the year 1998
1998 is not a leap year
Program 2: Check Leap Year
In this method, we will check whether the given year is a leap year or not using conditional operators.
Algorithm:
- Start
- Declare a variable let's say a year.
- Initialize it.
- Use a ternary operator to check whether the given year is a leap year or not.
- Call a method in the condition section of the ternary operator to check the same.
- Return true if the year is a multiple of 400.
- Else if the year is a multiple of 100, return false.
- Else if the year is a multiple of 4 then it is a leap year and return true. Else return false.
- Stop.
Below is the code for the same.
The below example demonstrates how to use a ternary operator to check for a leap year.
//Java Program to check whether the given year is a leap year or not
import java.util.Scanner;
public class CheckYear
{
static boolean checkLeapYear(int year)
{
// If a year is multiple of 400, then it is a leap year
if (year % 400 == 0)
return true;
// Else If a year is multiple of 100, then it is not a leap year
if (year % 100 == 0)
return false;
// Else If a year is multiple of 4, then it is a leap year
if (year % 4 == 0)
return true;
return false;
}
// Driver method
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int year; //Year Declaration
System.out.println("Enter the year");
year=sc.nextInt(); //Year Initialization
//Ternary Operator to check
System.out.println( checkLeapYear(2000)? "Leap Year" :
"Not a Leap Year" );
}
}
Enter the year 2012
Leap Year
Program 3: Check Leap Year
In this method, we will check whether the given year is a leap year or not using functions.
Algorithm:
- Start
- Declare a variable let's say year.
- Initialize it.
- Call a function to check.
- Return true if the year is a multiple of 400.
- Else if the year is a multiple of 100, return false.
- Else if the year is a multiple of 4 then it is a leap year and returns true.
- Using the if-else condition display the result.
- Stop.
Below is the code for the same.
The below example demonstrates how to use functions to check for a leap year.
//Java Program to check whether the given year is a leap year or not using Functions
import java.util.Scanner;
public class CheckYear
{
static boolean checkLeapYear(int year)
{
// If a year is multiple of 400,
// then it is a leap year
if (year % 400 == 0)
return true;
// Else If a year is multiple of 100,
// then it is not a leap year
if (year % 100 == 0)
return false;
// Else If a year is multiple of 4,
// then it is a leap year
if (year % 4 == 0)
return true;
return false;
}
// Driver method
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int year; //Year Declaration
System.out.println("Enter the year");
year=sc.nextInt(); //Year Initialization
boolean check=checkLeapYear(year);
if(check)
{
System.out.println(year+" is a leap year");
}
else
{
System.out.println(year+" is not a leap year");
}
}
}
Enter the year 2018
2018 is not a leap year