Leap Year Program In Java


Algorithm 1

Current year is a leap year or not

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import java.time.LocalDate;
public class IsLeapYear {
   public static void main(String args[]) {
      LocalDate currentDate = LocalDate.now();
      boolean isleapYear = currentDate.isLeapYear();
      if(isleapYear){
         System.out.println("Current year "+currentDate+" is a leap year ");
      }else{
         System.out.println("Current year "+currentDate+" is not a leap year ");
      }
   }
}

Algorithm 2

Take a year from the user and check whether it is Leap Year or not

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import java.time.LocalDate;
import java.util.Scanner;

public class IsLeapYear {
	public static void main(String args[]) {
		Scanner input = new Scanner(System.in);
		System.out.println("Enter the year: ");
		int year = input.nextInt();
		LocalDate givenDate = LocalDate.of(year, 01, 01);
		boolean isLeapYear = givenDate.isLeapYear();
		if (isLeapYear) {
			System.out.println("Given year " + year + " is a leap year ");
		} else {
			System.out.println("Given year " + year + " is not a leap year ");
		}
	}
}

Output
Enter the year: 
2018
Given year 2018 is not a leap year 

Algorithm 3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import java.util.Scanner;

public class IsLeapYear {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("Enter the year:");
		int year = input.nextInt();
		boolean condition1 = year % 4 == 0 && year % 100 != 0;
		boolean condition2 = year % 400 == 0;
		String result = condition1 || condition2 ? "Yes" : "No";
		System.out.println("Entered year " + year + " is a leap year: " + result);
	}
}

Output

Enter the year:
2022
Entered year 2022 is a leap year: No

Algorithm 4

 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
import java.util.Scanner;

public class IsLeapYear {
	public static boolean isLeapYear(int year) {
		if (year % 4 == 0) {
			if (year % 100 == 0) {
				if (year % 400 == 0)
					return true;
				else
					return false;
			}
			return true;
		}
		return false;
	}

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("Enter the year:");
		int year = input.nextInt();
		if (isLeapYear(year))
			System.out.println("Entered year " + year + " is a leap year ");
		else
			System.out.println("Entered year " + year + " is not a leap year ");
	}
}

Output

Enter the year:
2020
Entered year 2020 is a leap year 
Next Post Previous Post
No Comment
Add Comment
comment url