ADVERTISEMENT
ADVERTISEMENT

Java program to check whether year is Leap year or not

There are two conditions to check Leap year.

  1. For century years- year must be divisible by 400.
  2. For non century years- year must divisible by 4 (in 2nd condition after || operator, there is a condition year%100!=0 it means when year is not divisible by 100 that means it is non century years.

Check Leap Year using Java Program

//Java program to check whether year is Leap year or not.
 
import java.util.*;
 
class LeapYear
{
    public static void main(String []s)
    {
        int year;
        Scanner sc=new Scanner (System.in);
         
        try
        {
             
            System.out.print("Enter year:");
            year=sc.nextInt();
             
            if((year%400==0)||(year%100!=0 && year%4==0))
                System.out.println(year+" is a Leap Year.");
            else
                System.out.println(year+" is not a Leap Year.");
        }
        catch (Exception Ex)
        {
            System.out.println("Oops ... : " + Ex.toString());
        }
         
    }
}

Output

    Complie 	:	javac LeapYear.java
    Run		:	java LeapYear
    Output
    First Run:
    Enter year:2000
    2000 is a Leap Year.

    Second Run:
    Enter year:2100
    2100 is not a Leap Year.

    Third Run:
    Enter year:2004
    2004 is a Leap Year.

    Fourth Run:
    Enter year:2005
    2005 is not a Leap Year.

Java Basic Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.