Java/J2EE and SOA (324 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (323 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How to implement Java program to check Leap Year?

Published on Sep 09,2019 153 Views
8 / 29 Blog from Java Programs

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

I am pretty sure everyone of you must have heard about the term, Leap Year! A leap year has 366 days which occurs once in every 4 years. This additional day in the leap year is added in the month of February. Well, in this article I am going to brief you on how to implement leap year program in Java.

The agenda for this concept is going to be as follows:

Let’s Begin!

What is a Leap Year?

A leap year has 366 days. Now, there are certain conditions to check whether a year is a leap year or not! Let’s have a look at them:

  • The year should be divisible by 4.
  • If the year is divisible by 100, it is NOT a leap year, UNLESS, the year is divisible by 400 also. For instance; take the year, 2100: it is divisible by 100, hence according to the condition it should NOT be a leap year, but, the second part says if it is divisible by 400 too, it is a leap year! Hence, 2100 is a leap year, because it is divisible by 100 and subsequently by 400!

Now that you are well aware of the concept, let us implement it through a Java code.

Implement Leap Year Program in Java

Here’s a java code to implement leap year program in java:

Code:

class Test 
{ 
    static boolean Year(int year) 
    { 
         
        if (year % 400 == 0) 
            return true; 
       
        if (year % 100 == 0) 
            return false; 
      
         
        if (year % 4 == 0) 
            return true; 
        return false; 
    } 
          
     
    public static void main(String[] args)  
    { 
        int year = 2018; 
        System.out.println( Year(2018)? "Leap Year" : 
                           "Not a Leap Year" ); 
    } 
} 

Output:
Not a Leap Year

Here, you witnessed how easily we implemented our concept in Java.

Java Program Explanation

  1. Input the year which you want your code to be checked for leap year.
  2. The if statement then checks if the year is a multiple of 4 but not 100 or if it is a multiple of 400.
  3. The result is then printed.

I hope you are clear with the concept of Leap Year in Java now. Keep reading, keep exploring!

With this, we come to an end of this blog on “leap year program in java”. I hope it added value to your knowledge of java .

Now that you have understood Java code, check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this “leap year program in java” blog and we will get back to you as soon as possible. 

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.