Skip to main content

Java program to check whether year is leap year or not.

                            We are gonna check weather the given year is leap year or not. So first, what is leap year. How to write a program.

CONTENT.
  1. What is leap year?
  2. Logic for the program.
  3. Program.
  4. Explanation.
  5. Output.

What is leap year?

        Usually, we have 365 days per year but, the leap year is the year which has 366 days per year.

        It is said that leap is repeated every four year.

                    formula:  year%4=0

Logic for the program.
  • As we know leap year is repeated for every four year we need to divide the year we select by 4.
  • If the answer is 0, the year is leap year, if not it is not leap year.

Program.

import java.io.*;
class leap
{
 public static void main(String args[]) throws IOException
 {
  int y;
  InputStreamReader read=new InputStreamReader(System.in);
  BufferedReader in = new BufferedReader(read);
  System.out.println("Enter the year");
  y=Integer.parseInt(in.readLine());
    if(y%4==0) 
       System.out.println(y +" is a leap year");
      else
       System.out.println(y +" is not a leap year");
  }
}


Explanation.
  • We have given the class name has leap.
  • public static void main(String arg[]) refers to add a method i.e array of strong method to the program.
  • int y is declaring the variable.
  • InputStreamReader read = new InputStreamReader(System.in); we create this object because we need to give the value to variable at the time of execution, this is a one of the way to give value to a variable.
  • BufferedReader in = new BufferedReader(read); we create this object because, we need a buffer to store the value of the variable.
  • n=Integer.parseInt(in.readLine()) is used to convert array of numbers to primitive integer.
  • Now, we have to give condition which satisfies all the above int the logic for the program. I choose, if statement to satisfy the condition.
  • y%4==0 is the condition.
  • System.out.println is used to display the given in output screen(make sure S in System is capital)

Know more about if statement here.


Output:

       Compile : javac leap.java
       Run        : java leap
       Enter the year
       2020
       2020 is a leap year.

      Compile : javac leap.java
       Run        : java leap
       Enter the year
       2525
       2525 is not a leap year.






Comments