java

Java Leap Year Program|Java Program-5 ways

Java Leap Year Program|Java Program

In this article, we will discuss the concept of Java Leap Year Program.

In this Program, we are going to learn how to make sure the given year whether leap year or not using different methods in Java language.

This is done using if-else , if-else if-else, Nested -if,ternary operator and method in Java language.

Leap Year Program

Check whether the given year is leap or not- using if-else

In this program, we are going to learn how to make sure the given year whether 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);
  
  System.out.println("Please Enter year for check leap: ");
int year=sc.nextInt();
boolean is_Leap=false;

if ((year%400==0)||((year%4==0)&&(year%100!=0))){
       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 year for check leap:
2000
2000 is a leap year

case 2

Please Enter year for check leap:
1994
1994 is not a leap year

 

Check whether the given year is leap or not- using if-else if

In this program, we are going to learn how to make sure the given year whether leap year or not using if-else if statements in Java language

Program 2

import java.util.Scanner;
public class Check_Leap_Year2{
public static void main(String args[]){
  
  Scanner sc=new Scanner(System.in);
  
  System.out.println("Please Enter year for check leap: ");
int year=sc.nextInt();
boolean is_Leap=false;

if(year%400==0){
   System.out.println(year+" is a leap year ");
}
else if(year%100==0){
  System.out.println(year+" is not a leap year ");
}
  
   else if(year%4==0){
      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 year for check leap:
2020
2020 is a leap year

case 2

Please Enter year for check leap:
2100
2100 is not a leap year

 

Check whether the given year is leap or not- using Nested if

In this program, we are going to learn how to make sure the given year whether leap year or not using Nested if statements in Java language

Program 3

import java.util.Scanner;
public class Check_Leap_Year{
public static void main(String args[]){
  
  Scanner sc=new Scanner(System.in);
  
  System.out.println("Please Enter year for check leap: ");
int year=sc.nextInt();
boolean is_Leap=false;

if(year%4==0){
     if(year%100==0){
           if(year%400==0){
      is_Leap=true;
        }else
    is_Leap=false;
    } else
    is_Leap=true;
  }else
    is_Leap=false;
  
if(is_Leap==true)
       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 year for check leap:
2024
2024 is a leap year

case 2

Please Enter year for check leap:
2030
2030 is not a leap year

 

Check whether the given year is leap or not- using ternary operator

In this program, we are going to learn how to make sure the given year whether leap year or not using ternary operator in Java language

Program 4

import java.util.Scanner;
public class Check_Leap_Year3{
public static void main(String args[]){
  
  Scanner sc=new Scanner(System.in);
  
  System.out.println("Please Enter year for check leap: ");
int year=sc.nextInt();
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 year for check leap:
2024
The entered year is a leap year

Case 2

Please Enter year for check leap:
2030
This is not a leap year

 

Check whether the given year is leap or not- using method

In this program, we are going to learn how to make sure the given year whether leap year or not using user defined method in Java language

Program 5

import java.util.Scanner;
public class Check_Leap_YearMethod{
public static void main(String args[]){
  
  Scanner sc=new Scanner(System.in);
  
  System.out.println("Please Enter year for check leap: ");
int year=sc.nextInt();
findLeap(year);
}

static void findLeap(int year){
if(year!=0){
if(year%400==0){
   System.out.println(year+" is a leap year ");
}
else if(year%100==0){
  System.out.println(year+" is not a leap year ");
}
  
   else if(year%4==0){
      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 year for check leap:
2024
2024 is a leap year

Case 2

Please Enter year for check leap:
2222
2222 is not a leap year

 

Suggested for you

if statements in Java language

Nested if in Java language

Data type and variable in Java

Operator in Java language

Method in Java language

 

Similar post

C Leap Year Program|C Program

C++ Leap Year Program|C++ Program

Python Leap Year Program|Python Program

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.