Write a program that determines whether a given year is a leap year. Leap years occur every 4 years, with some exceptions:
For example:
This challenge is a great way to practice conditional logic and modulo operations. It also demonstrates how logical rules can translate into code. Understanding leap years is an essential problem-solving exercise, often used in real-world applications like calendar systems and date validation.
The rules for identifying a leap year are:
print("Welcome to the Leap Year Checker!")
while True:
try:
# Get the year as input
year = int(input("Enter a year (or type -1 to quit): "))
if year == -1:
print("Goodbye!")
break
# Check if it's a leap year
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
except ValueError:
print("Please enter a valid integer.")
import java.util.Scanner;
public class LeapYearChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the Leap Year Checker!");
while (true) {
System.out.print("Enter a year (or type -1 to quit): ");
int year = scanner.nextInt();
if (year == -1) {
System.out.println("Goodbye!");
break;
}
// Check if it's a leap year
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
}
}
console.log("Welcome to the Leap Year Checker!");
while (true) {
let input = prompt("Enter a year (or type -1 to quit):");
if (input === "-1") {
console.log("Goodbye!");
break;
}
let year = parseInt(input);
if (isNaN(year)) {
console.log("Please enter a valid integer.");
continue;
}
// Check if it's a leap year
if ((year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0)) {
console.log(`${year} is a leap year.`);
} else {
console.log(`${year} is not a leap year.`);
}
}
In Day 25: Number Guessing Game, you’ll create an interactive Number Guessing Game where the computer generates a random number, and the user tries to guess it.