Java Program To Check the Year is Leap Year or Not

In this post, telling about the how to check or how to find a year is leap year or not. Before reading or writing the java code, First do you have know that, What is leap year ? and how many days are there in one leap year ? .  Leap year and non leap year there are two types of year. The one leap year is 366 days and one non leap year is 365 days. In every 4 years one is leap year and other three year are non leap year. In leap year February month is 29 days and non leap year February month is 28 days.

java-program-to-check-leap-year

Java code:

import java.util.Scanner
class Leapyear
{
 public static void main (String args[])
 {
  Scanner sc = new Scanner (System.in)
  int year;
  System.out.println("Enter any year : ");
  year = sc.nextInt();
  if(year%4 == 0)
  {
   System.out.println("This year is a Leap year.");
  }
  else
  {
   System.out.println("This year is not a Leap year.");
  }
 }
}

Output-1

Enter any year : 2022
This year is not a Leap year
.

Output-2

Enter any year : 2024
This year is a Leap year.

Explanation :

Step:1
First create a class. In this program create one class and the class name is Leapyear .
Step:2
Under the class, create a main function.
Step:3
Under the main function, create an object of the scanner class and the object name is sc.
Step:4
Declare one integer variable, that name is year. This variable store the user input using the nextInt() method of the scanner class.
Step:5
Use conditional statement(if/else) to check year is leap year or not. If (year%4 == 0) So, that condition is true and this year is leap year. else condition is false so, year is not a leap year. Why use year % 4 because every 4 year one year is leap year. In output:1 , user input year is 2022. (2022 % 4 = 2) So, that result is false and output is not a leap year. In output:2 , user input year is 2024. (2024 % 4 = 0) So, that result is true and output is leap year.

Thank You.

Post a Comment

0 Comments