Home Java Beginners Listing out leap years between certain period

 
 

Share on Google+Share on Google+

Listing out leap years between certain period

Advertisement
The programming lesson will teach you the coding for finding and listing out the leap years between two years.

Listing out leap years between certain period

     

The programming lesson will teach you the coding for finding and listing out the leap years between two years. In the following example we have to find out the leap years between 1990 and 2006. First define the two years under a class "leapyears". Let i = 2006 and n=1990. Now with the help of for loop method initialize the year as n=1990 and n<=i. Also apply the increment statement in the loop as we have to check one by one. 

As we know a leap year is divisible by 4, define an integer l=n%4. So if 'n' is divisible by 4 or l=0, then the particular year can be a leap year. For checking this, apply the if statement and if this satisfies then, the year will be a leap year. For listing out each year write "+n" in the System.out.println. 

Now compile and run the program in the command window and see the result. If you find any error, check the whole program and find out the   

Here is the code of the program:

class leapyears 
{
  public static void main(String[] args
  {
  int i=2006;
  int n;
  for (n=1990; n<=i ; n++){
  int l=n%4;
  if (l==0){
  System.out.println("leap year: "+n);
  }
  }
  }
}

Download this 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: Listing out leap years between certain period   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
satendra
August 16, 2011
leap year

finding given year is leap year or not
manimegali
March 14, 2012
java

how to calculate no of days a man lived from his birth date upto today............
DMCA.com