Codeforcoding

Java code to check if the given year is a leap using method

Java code to check if the given year is a leap using method

In this article, we will discuss the concept of Java code to check if the given year is a leap or not using method

In this post, we are going to learn how to write a program to check and display  the given year is leap or not using Java method in  Java programming language. 

Java code to check if the given year is a leap using method
Check if the given year is a leap

Code to find if the given year is a leap

What is leap year

A normal year contains 365 days but a leap year contains 366 days

leap year is occurring once every four years, which has 366 days including 29 February as an intercalary day. it is called leap year

Century year

The century year is ending with 00

Ex 2000, 2100, 2200

a century year may be a leap year but it is not compulsory

 

How to find the leap year

if The given year is perfectly divisible by 4 , it is a leap year(except century year)

only if it is perfectly divisible by 400, the century year is a leap year,
a century year should be divisible by 4 and 100 both
a non century year should be divisible only by 4

 

Program to check leap year using method

Here, The program allows the user to enter the year and then it will check the year is a leap year or not, using method in Java language

Program 1

import java.util.Scanner;
public class Check_LeapYear_Method{
public static void main(String args[]){
    
    Scanner sc=new Scanner(System.in);
    //Scanner object for input
    System.out.println("Please Enter year for check leap: ");
    //Ask input from the user
int year=sc.nextInt();
//store the input in the year variable

check_Leap(year);//Calling the method
}

static void check_Leap(int year){//Method definition
if(year!=0){
    //if the year is divided by 400
if(year%400==0){//if the year is divided by 400
     System.out.println(year+" is a leap year ");
}
else if(year%100==0){//if the year is divided by 100
    System.out.println(year+" is not a leap year ");
}
    
   else if(year%4==0){//if the year is divided by 4
        System.out.println(year+" is a leap year ");
   }
       else{
       System.out.println(year+" is not a leap year ");
}
}
}
}

When the above code is executed, it produces the following result

Case 1

Please Enter year for check leap:
2024
2024 is a leap year

 

Case 2

Please Enter year for check leap:
2030
2030 is not a leap year


Suggested post

if statements inn Java language

Nested if statements in Java language

Data type and variable in Java

Operator in Java language

Class and main method in Java language

 

Similar post

Java program to find leap year

C program to find leap year

C++ program to find leap year

Python program to find leap year

C++ program for check whether given year is leap using function

Python program for check whether given year is leap using function

C program for check whether given year is leap using function

 

C++ program for check whether given year is leap using function
C program for check whether given year is leap using function
Exit mobile version