Write a program that checks whether a user-specified year is a leap year. The ru
ID: 3663991 • Letter: W
Question
Write a program that checks whether a user-specified year is a leap year. The rules for a leap year are as follows (from scienceworld.wolfram.com): In the Gregorian calendar currently in use worldwide (except perhaps the Russian and Iranian calendars), there is a leap year every year divisible by four except for years which are both divisible by 100 and not divisible by 400. Therefore, the year 2000 was a leap year, but the years 1700, 1800, and 1900 were not. Write a program that asks the user for a year, checks that the number is positive, and then determines whether or not the year is a leap year. If the year is not a leap year, the program should specify the next year that will be a leap year.
Explanation / Answer
Java Program:
/*A leap year in the Gregorian calendar has an extra day for February.
*A leap year has 366 days.
*
* Algorithm to find a leap year
* -------------------------------
* if year % 400 = 0, leap year
* else if year % 100 = 0, not a leap year
* else if year % 4 = 0, leap year
* else, not a leap year
*/
public class LeapYearCalculator
{
public static void main(String args[])
{
/*Give the year here which needs to be checked for leap year
* (Assuming that year given here >= 1582 and corresponds to a year in Gregorian calendar)
*/
int year;
Scanner input= new Scanner(System.in);
System.out.println("enter the year, which you want to know,is a leap year or not");
year= input.nextInt();
//Flag to store the test result
boolean isLeapYear = false;
if(year % 400 == 0)
{
isLeapYear = true;
}
else if (year % 100 == 0)
{
isLeapYear = false;
}
else if(year % 4 == 0)
{
isLeapYear = true;
}
else
{
isLeapYear = false;
}
//Output the test result
if(isLeapYear)
{
System.out.println("Year "+year+" is a Leap Year");
}
else
{
System.out.println("Year "+year+" is not a Leap Year");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.