Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- int year;
- boolean isLeapYear = false;
- Scanner scan = new Scanner(System.in); // Initialize a Scanner with system InputStream
- System.out.println("----Program checking the leap year----");
- while (true) {
- System.out.println("Enter year: ");
- try {
- year = scan.nextInt(); // Wait to read user input to Integer/int
- } catch (Exception e) {
- scan.nextLine(); // use this to clear previous scan.next.. buffer
- continue;
- }
- if (year % 4 == 0) { // divisible 4 - leap year
- if (year % 100 == 0) { // divisible both 4 and 100 is not leap year
- if (year % 400 == 0) // divisible 400 is leap year
- isLeapYear = true;
- else
- isLeapYear = false; // not divisible 400 is not leap year
- } else { // divisible 4 but not divisible 100 - leap year
- isLeapYear = true;
- }
- } else { // not leap year
- isLeapYear = false;
- }
- if (isLeapYear)
- System.out.println(year + " is a leap year.");
- else
- System.out.println(year + " isn't a leap year.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement