-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathLeap Year Program in Java
35 lines (29 loc) · 1.04 KB
/
Leap Year Program in Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
// Create a Scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a year
System.out.print("Enter a year: ");
// Read the year from the user
int year = scanner.nextInt();
// Check if the year is a leap year
if (isLeapYear(year)) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
// Close the scanner
scanner.close();
}
// Function to check if a year is a leap year
public static boolean isLeapYear(int year) {
// A year is a leap year if it is divisible by 4
// but not divisible by 100, or if it is divisible by 400
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return true;
} else {
return false;
}
}
}