Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,226 questions

56,128 answers

573 users

How to check leap year in Java

2 Answers

0 votes
import java.util.Scanner;
 
public class JavaApplication1 
{
    public static void main(String[] args) 
    {
        try
        {
            Scanner in = new Scanner(System.in);
            System.out.print("Enter a year: ");
            int year = in.nextInt();
           
            if (year % 400 == 0)
                System.out.format("%d is a leap year.\n", year);
            else if (year % 100 == 0)
                     System.out.format("%d is not a leap year.\n", year);
                 else if (year % 4 == 0)
                          System.out.format("%d is a leap year.\n", year);
                      else
                          System.out.format("%d is not a leap year.\n", year);  
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        } 
    }
}


 
/*
 
run:
 
Enter a year: 2016
2016 is a leap year.
 
*/


answered May 24, 2015 by avibootz
edited Jul 25, 2022 by avibootz
0 votes
public class JavaApplication1 
{
    public static void main(String[] args) 
    {
        try
        {
            int[] years = {2000, 2400, 1800, 1900, 2100, 2200, 2300, 2500,
                           2008, 2012, 2016, 2020, 2024, 2048, 2032};
  
                for (int yr : years)
                {
                    if (isLeapYear(yr))
                        System.out.format("%d is a leap year.\n", yr);
                    else
                        System.out.format("%d is not a leap year.\n", yr);  
                }
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }  
    }
    static boolean isLeapYear(int year)
    {
        if (year % 400 == 0)
            return true;
        else if (year % 100 == 0)
                 return false;
        else if (year % 4 == 0)
                 return true;
  
        return false;
    }
}
 
 
 
 
/*
 
run:
 
2000 is a leap year.
2400 is a leap year.
1800 is not a leap year.
1900 is not a leap year.
2100 is not a leap year.
2200 is not a leap year.
2300 is not a leap year.
2500 is not a leap year.
2008 is a leap year.
2012 is a leap year.
2016 is a leap year.
2020 is a leap year.
2024 is a leap year.
2048 is a leap year.
2032 is a leap year.
 
*/


answered May 24, 2015 by avibootz
edited Jul 25, 2022 by avibootz

Related questions

3 answers 273 views
273 views asked Mar 18, 2021 by avibootz
1 answer 214 views
1 answer 159 views
159 views asked Dec 12, 2024 by avibootz
1 answer 154 views
154 views asked Dec 12, 2024 by avibootz
1 answer 159 views
159 views asked Dec 12, 2024 by avibootz
1 answer 141 views
141 views asked Dec 12, 2024 by avibootz
1 answer 155 views
155 views asked Sep 26, 2024 by avibootz
...