Java Check for Leap Year

Question:-

Given a year, check if the year is leap year or not. If yes, the output should be “Leap Year”.  Else output should be “Not a Leap Year”.  The input should be a positive four digit number.  Else,  the output should be “Invalid Year”.

Sample Input  1 :

Enter the Year

2016

Sample Output  1 :

Leap Year

Sample Input  2 :

Enter the Year

2001

Sample Output  2 :

Not a Leap Year

Code:-

import java.util.Scanner;
public class LeapYear
{
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the Year");
        
        int yr=sc.nextInt();
        
        if(yr>999 && yr<10000)
        {
            if(yr%4==0)
            {
                if(yr%100==0)
                {
                    if(yr%400==0)
                    {
                        System.out.println("Leap Year");
                    }
                    else
                    {
                        System.out.println("Not a Leap Year");
                    }
                }
                else
                {
                    System.out.println("Leap Year");
                }
            }
            else
            {
                System.out.println("Not a Leap Year");
            }
        }
        else
        {
            System.out.println("Invalid Year");
        }
    }
}

Recommended:-

Leave a Comment

Your email address will not be published. Required fields are marked *