Home Java Beginners Checking whether a year is leap or not

 
 

Share on Google+Share on Google+

Checking whether a year is leap or not

Advertisement
This tutorial is going to teach you the coding for checking whether a year is a leap year or not.

Checking whether a year is leap or not

     

This tutorial is going to teach you the coding for checking whether a year is a leap year or not. Here, we have taken the year 2000. So define an integer n=2000 in the class "Leapyear" and now apply "if else" condition. As we know leap year is divided by the integer 4 and so applying if condition as n/4=0, then "n" is a leap year. Now in the System.out.println write the message that the year is a leap year. Again applying "else" condition the output will be that the year is not a leap year. 

 

Here is the code of program:

 

 

class  Leapyear
{
  public static void main(String[] args
  {
  int n=2000;
  if (n%4==0){
  System.out.println("The given year is a leap year");
  }
  else{
  System.out.println("This is not a leap year");
  }
}
}

Download the program:

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 11, 2008

Related Tutorials

Discuss: Checking whether a year is leap or not   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:9
Vajrala Rama Rao
June 25, 2011
wrong program

Please verify your program. Logic Is Incorrect.
Naresh
September 13, 2011
Wrong Logic

Logic has to be if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) System.out.println("Year " + year + " is a leap year"); else System.out.println("Year " + year + " is not a leap year");
neeraj
September 27, 2011
Bug

this is wrong.....Because 100 is not a leap year Correct logic is if(year%400 ==0 || (year%100 != 0 && year%4 == 0)) { printf("Year %d is a leap year",year); }
Dinesh
November 1, 2011
java Program to find a given year is leap or not?

import java.util.Scanner; public class LeapYearOrNot { public static void main(String[] args) { int n; System.out.println("Enter the year"); Scanner sc = new Scanner(System.in); n = sc.nextInt(); if (n%4 == 0 && n%100!=0||n%400==0) { System.out.println("The given year is a leap year"); } else { System.out.println("This is not a leap year"); } } }
Dni
November 1, 2011
hi,

hi very thankful to u bcz by this prog i got an idea..
dinesh kumar girare
March 13, 2013
leap year

the logic of leap year if(year%4==0) is perfectly not a correct logic so change these correct logic if((year%4==0)&&year%100!=0) { System.out.println("leap year"); } else { if((year%4==0)&&(year%400==0)&&(year%100==0) System.out.println("leap year"); else System.out.println("not leap year"); } } so thanks
brycool
September 21, 2012
java

how to create the leap year program in java
Pritam Patra
November 25, 2012
The same modified with users own input.

package prix; import java.util.Scanner; public class LeapYear { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the year : "); int n = scanner.nextInt(); if (n%4==0){ System.out.println("Congratz! This is a leap year"); } else{ System.out.println("This is not a leap year"); } } }
rishipalyadav
December 3, 2013
java program

java program not find intheweb
DMCA.com