Sitemap
CodeForHunger

CodeForHunger

Here, I will share programming language tutorials and tech updates. Learn C and Java with tutorials, coding examples & interview questions. Follow for more...

Leap Year Program In Java

2 min readJun 6, 2024

Java Program to Find Year is Leap Year or Not

In this post, we are going to learn how to find whether a given year is a leap year or not using Java. Writing a code to find whether a given year is a leap year or not is a standard Java programming exercise during various programming language courses, colleges, and training institutes. These types of programs are also commonly asked in programming competitions to test your logical and problem-solving skills. Before diving in, we must understand what a leap year is.

What is a leap year?

We all know that in a regular year, there are 365 days. However, in a leap year, there is an extra day, meaning a leap year has 366 days. This extra day comes in February month, that means In leap year Feb month has 29 days than the normal 28 days. Leap year comes once every four years.

Step by Step logic to check leap year:

  1. Accept the input year from the user.
  2. Write the following logic inside an if statement to check given year is a leap year or a normal year.
if((year%400==0) ||(year%100!=0 && year%4==0))

3. If the above condition is true that means the given year is a Leap year. Otherwise, print “Given year is not a leap year”.

Get CodeForHunger’s stories in your inbox

Join Medium for free to get updates from this writer.

Java Program to Check If Year is Leap Year or Not:

import java.util.Scanner;

public class LeapYearProgram
{
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the Year: ");
int year = sc.nextInt();

if((year%400==0) ||(year%100!=0 && year%4==0)){
System.out.println("Given Year is a Leap Year...");
}else {
System.out.println("Given Year is Not a Leap Year...");
}
}
}

Above program will print the following output:

Enter the Year: 2024

Given Year is a Leap Year…

Happy coding… :)

--

--

CodeForHunger
CodeForHunger

Written by CodeForHunger

Here, I will share programming language tutorials and tech updates. Learn C and Java with tutorials, coding examples & interview questions. Follow for more...