Java program to check the given year is a leap year
- Home
- Check value
- Java program to check the given year is a leap year
- On
- By
- 0 Comment
- Categories: Check value, If block
Java program to check the given year is a leap year
Java program to check if the given year is a leap year
In this article, we will discuss the concept of Java program to check if a given year is a leap year
In this post, we are going to learn how to write a program to check the given year is leap or not in Java programming language. using if block
What is leap year
A normal year contains 365 days but a leap year contains 366 days
leap year is occurring once every four years, which has 366 days including 29 February as an intercalary day. it is called leap year
Century year
The century year is ending with 00
Ex 2000, 2100, 2200
a century year may be a leap year but it is not compulsory
How to find the leap year
if The given year is perfectly divisible by 4 , it is a leap year(except century year)
only if is perfectly divisible by 400, the century year is a leap year,
a century year should be divisible by 4 and 100 both
a non century year should be divisible only by 4
program to check leap year using if else
Here, The program allows the user to enter the year and then it will check the year is a leap year or not, using if – else statements in Java language
Program 1
import java.util.Scanner; public class Check_Leap_Year1{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); //Ask input from the user System.out.println("Please Enter year for check leap: "); int year=sc.nextInt(); boolean is_Leap=false; //Check leap year in one line if ((year%400==0)||((year%4==0)&&(year%100!=0))){ //if it is true display leap year //if it is false move to else part System.out.println(year+" is a leap year "); } else{ System.out.println(year+" is not a leap year "); } } }
When the above code is executed, it produces the following result
Case 1
Please enter for check leap or not 2016 2016 is a leap year
Case 2
Please enter for check leap or not 2022 2022 is a not a leap year
program to check leap year using Nested if else
Here, The program allows the user to enter the year and then it will check the year is a leap year or not using Nested if – else statements in Java
Program 2
import java.util.Scanner; public class Find_Leap_Year{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); System.out.println("Please Enter year for check leap or not: "); int year=sc.nextInt(); boolean is_Leap=false; //if the years is devided by 4 if(year%4==0){ //if the years is century if(year%100==0){ //if the years is devided by 400 //if all the above conditionsa is true, it is a leap year if(year%400==0){ is_Leap=true; }else is_Leap=false; //if the years is not century } else is_Leap=true; }else is_Leap=false; if(is_Leap==true) System.out.println(year+" is a leap year "); //display leap year else System.out.println(year+" is not a leap year "); } }
When the above code is executed, it produces the following result
Case 1
Please enter for check leap or not 1244 1244 is a leap year
Case 2
Please enter for check leap or not 1366 1366 is a not a leap year
Program to check leap year using if else if else
Here, The program allows the user to enter the year and then it will check the year is a leap year or ,using if – else-if else statements in Java language
Program 3
import java.util.Scanner; public class find_Leap_Year2{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); System.out.println("Please Enter year for check leap or not: "); int year=sc.nextInt(); boolean is_Leap=false; //if years is divided by 400 if(year%400==0){ System.out.println(year+" is a leap year "); } //if the year is century else if(year%100==0){ System.out.println(year+" is not a leap year "); } //if the year is devided by 4 else if(year%4==0){//then it is a leap year System.out.println(year+" is a leap year "); } else{ System.out.println(year+" is not a leap year "); } } }
When the above code is executed, it produces the following result
Case 1
Please enter for check leap or not 2020 2020 is a leap year
Case 2
Please enter for check leap or not 2006 2006 is a not a leap year
program to check leap year using ternary operator
Here, The program allows the user to enter the year and then it will check the year is a leap year or not using ternary operator in Java language
Program 4
import java.util.Scanner; public class find_Leap_Year3{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); System.out.println("Please Enter year for check leap: "); //Ask input from the user int year=sc.nextInt(); //store the in the year variable entered by user String leap=(year%4==0 || year%100!=0 && year%400==0)? "The entered year is a leap year":"This is not a leap year"; System.out.println(leap); } }
When the above code is executed, it produces the following result
Case 1
Please enter for check leap or not 2020 The entered year is a leap year
Case 2
Please enter for check leap or not 2022 This is not a leap year
Suggested post
if statements inn Java language
Nested if statements in Java language
Data type and variable in Java
Operator in Java language
Class and main method in Java language
Similar post
Java program to find leap year
Python program to find leap year