Check for Leap Year Program in java



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");
        }
    }
}

 


Tags:

java interview programs for experienced | java interview programs for freshers pdf | java coding questions for 5 years experience | java programming interview questions – geeksforgeeks |
java programming interview questions and answers for freshers | java programs asked in interview for automation tester | java coding interview questions for experienced professionals
| java coding interview questions pdf

Previous
Next Post »