35.5k views
7 votes
A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun. To account for the difference in time, every 4 years, a leap year takes place. A leap year is when a year has 366 days: An extra day, February 29th. The requirements for a given year to be a leap year are:

1) The year must be divisible by 4

2) If the year is a century year (1700, 1800, etc.), the year must be evenly divisible by 400

Some example leap years are 1600, 1712, and 2016.

Write a program that takes in a year and determines whether that year is a leap year.

Ex: If the input is:

1712
the output is:

1712 - leap year
Ex: If the input is:

1913
the output is:

1913 - not a leap year
LABACTIVITY

5.24.1: LAB: Leap year

0 / 10

main.py

Load default template...

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

else

isLeapYear = false;

}

else

isLeapYear = true;

}

else

isLeapYear = false;

if(isLeapYear)

System.out.println(inputYear + " is a leap year.");

else

System.out.println(inputYear + " is not a leap year.");

}

User Mus
by
4.4k points

1 Answer

3 votes

Final answer:

A program to determine whether a year is a leap year in the Gregorian Calendar would check if the year is divisible by 4, and for century years, it would additionally check if they are divisible by 400. This ensures the calendar year maintains synchronicity with the Earth's orbit around the Sun over long periods.

Step-by-step explanation:

The Gregorian Calendar, introduced by Pope Gregory XIII in 1582, corrected inaccuracies in the Julian calendar by adjusting the rules for determining leap years. The key change was to exclude century years from being leap years unless they were divisible by 400, thereby creating a calendar year that averages 365.2425 days, which matches closely with the tropical year of approximately 365.2422 days. The need for a leap year arises because the Earth's orbit around the Sun takes approximately 365.2422 days, rather than a neat 365. Therefore, without correction, the calendar year would drift from the astronomical events it was intended to synchronize with, like the equinoxes and solstices.

To write a program to determine whether a year is a leap year, one would need to check the following: if the year is divisible by 4, it's a leap year unless it is a century year not divisible by 400. Here is a simple pseudo-code example:

if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
isLeapYear = true;
} else {
isLeapYear = false;
}
} else {
isLeapYear = true;
}
} else {
isLeapYear = false;
}

Using this logic, the program can accurately determine whether a given year is a leap year and print the appropriate output, as requested in the question.

User Seihyung Oh
by
4.2k points