Tech Study

Write a Java method to check whether an year (integer) entered by the user is a leap year or not

Leap year program in JAVA – To find whether a year is a leap or not is a bit tricky. We assume that if a year number is completely divisible by 4 is a leap year. But it is not the only subtle case. A year is a leap year if −

  • 1. It is evenly divisible by 100
  • 2. If it is divisible by 100 as well as by 400
  • 3. Despite this, all other years evenly divisible by 4 are leap years.

Let’s discuss the algorithm:
1. Take an integer variable for the year

  1. Now assign a value to the variable.
  2. Check whether the year is divisible by 4 but not 100, DISPLAY “leap year”
  3. Else check if the year is divisible by 400, DISPLAY “leap year”
  4.  Otherwise, DISPLAY “not leap year”

Code :

import java.util.Scanner;

public class LeapYear {

public static void main(String[] args) {

     int year;

     System.out.println("Enter an Year :: ");

     Scanner sc = new Scanner(System.in);

        year = sc.nextInt();

     if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) System.out.println("Specified year is a leap year");

     else

         System.out.println("Specified year is not a leap year");

}

}

For example – Check for year 2020

OUPUT: 

Enter a Year

2020

Specified year is a leap year.

TaggedWrite a Java method to check whether an year (integer) entered by the user is a leap year or not

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more