Wednesday, 22 August 2018

Java Program to Check Leap Year



Java Program to Check Leap Year

In this program, you'll figure out how to check if the given year is a jump year or not. This is checked utilizing an if else explanation.

A leap year is precisely detachable by 4 aside from century (years finishing with 00). The century year is a jump year just in the event that it is flawlessly distinguishable by 400.

Example: Java Program to Check a Leap Year
public class LeapYear {

    public static void main(String[] args) {

        int year = 1900;
        boolean leap = false;

        if(year % 4 == 0)
        {
            if( year % 100 == 0)
            {
                // year is divisible by 400, hence the year is a leap year
                if ( year % 400 == 0)
                    leap = true;
                else
                    leap = false;
            }
            else
                leap = true;
        }
        else
            leap = false;

        if(leap)
            System.out.println(year + " is a leap year.");
        else
            System.out.println(year + " is not a leap year.");
    }
}
When you run the program, the yield will be: 

1900 isn't a jump year. 

When you change the estimation of year to 2012, the yield will be: 

2012 is a jump year. 

In the above program, given year 1900 is put away in the variable year.

Since 1900 is divisble by 4 and is likewise a century year (finishing with 00), it has be divisble by 400 for a jump year. Since it's not distinguishable by 400, 1900 isn't a jump year.

However, in the event that we change year to 2000, it is detachable by 4, is a century year and is likewise distinguishable by 400. In this way, 2000 is a jump year.

In like manner, If we change year to 2012, it is distinguishable by 4 and isn't a century year, so 2012 a jump year. We don't have to check if 2012 is detachable by 400 or not.

Here we will compose a java program to check whether the info year is a jump year or not. Before we see the program, lets perceive how to decide if a year is a jump year numerically:

To decide if a year is a jump year, take after these means:

1. In the event that the year is equitably separable by 4, go to stage 2. Something else, go to stage 5.

2. In the event that the year is equitably separable by 100, go to stage 3. Something else, go to stage 4.

3. In the event that the year is equitably separable by 400, go to stage 4. Something else, go to stage 5.

4. It is a jump year (it has 366 days).

5. The year isn't a jump year (it has 365 days). Wellspring of these means.

Case: Program to check whether the info year is jump or not

Here we are utilizing Scanner class to get the contribution from client and afterward we are utilizing if-else proclamations to compose the rationale to check jump year. To comprehend this program, you ought to have the learning of following ideas of Core Java Tutorial:

? On the off chance that else explanation 

? Read input number in Java program

import java.util.Scanner;
public class Demo {

    public static void main(String[] args) {

    int year;
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter any Year:");
    year = scan.nextInt();
    scan.close();
        boolean isLeap = false;

        if(year % 4 == 0)
        {
            if( year % 100 == 0)
            {
                if ( year % 400 == 0)
                    isLeap = true;
                else
                    isLeap = false;
            }
            else
                isLeap = true;
        }
        else {
            isLeap = false;
        }

        if(isLeap==true)
            System.out.println(year + " is a Leap Year.");
        else
            System.out.println(year + " is not a Leap Year.");
    }
}
Output:
Enter any Year: 
2001
2001 is not a Leap Year.

0 comments:

Post a Comment