-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay2.java
More file actions
31 lines (27 loc) · 1.3 KB
/
Copy pathDay2.java
File metadata and controls
31 lines (27 loc) · 1.3 KB
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
/*
Write a Java program that takes a year from the user and prints whether it is a leap year or not.
*/
// Importing the Scanner class to take input from the user
import java.util.Scanner;
public class Day2 {
public static void main(String[] args) {
// Creating a Scanner object to take input from the user
Scanner input = new Scanner(System.in);
// Prompting the user to enter the year
System.out.print("Input the year: ");
// Reading the user's input and storing it in the 'year' variable
int year = input.nextInt();
// Computing logic using help from flowchart
boolean x = (year % 4 == 0); // Checking if the year is divisible by 4
boolean y = (year % 100 != 0); // Checking if the year is not divisible by 100
boolean z = ((year % 100 == 0) && (year % 400 == 0)); // Checking if the year is divisible by 100 and also divisible by 400
// Checking the conditions for a leap year
// If Divisible by 4 and not divisible by 100 or divisible by 100 and 400 then a leap year
if ((x & y) || (y && z)){
System.out.println(year + " is a Leap Year");
} else {
System.out.println(year + " is not a Leap Year");
}
input.close();
}
}