Difference between revisions of "Leap Year Problem"

From CompSciWiki
Jump to: navigation, search
Line 39: Line 39:
 
             if( input != null )
 
             if( input != null )
 
             {
 
             {
                 year = Integer.parseInt( input );
+
                 year = Integer.parseInt( input ); //need to parse the user input as an integer
 
                  
 
                  
 
                 //first, check if it's a leap year. For it to be a leap year,  
 
                 //first, check if it's a leap year. For it to be a leap year,  
Line 55: Line 55:
 
             }
 
             }
 
             else //the user canceled
 
             else //the user canceled
 +
            {
 
                 done = true;
 
                 done = true;
 
             }
 
             }

Revision as of 23:17, 5 April 2010

Back to the Program-A-Day homepage

Problem

Write a short program that takes a year as input from the user, and then determines whether or not that particular year is (or was) a leap year. Your program should keep asking for more years until the user enters something non-numeric, or clicks 'Cancel' on the input box.

As a reminder, here is the criteria that dictates whether or not a year is a leap year: If the year is evenly divisible by 4, then the year is a leap year -- unless it is also divisible by 100, in which case it is not. An exception to this is if the year is divisible by 400, then it is still a leap year.

Examples:
1996 was a leap year because it was divisible by 4.
1900 was not a leap year in spite of being divisible by 4, because it is also divisible by 100.
2000 was a leap year because it is divisible by 400.
2009 was not a leap year, because it is not divisible by 4.

 

SideSectionTitle

float
Taken from http://www.flickr.com/photos/daniello/565304023/

An image or By Students section

Solution

The solution...

Code

Solution Code

Back to the Program-A-Day homepage