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


Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. import java.util.Scanner;
  2.  
  3. public class Javaexcercise {
  4.  
  5. public static void main(String[] args)
  6. {
  7. Scanner in = new Scanner(System.in);
  8. System.out.print("Enter a year: ");
  9. int year = in.nextInt();
  10.  
  11. System.out.println(checkLeapYear(year));
  12. }
  13.  
  14. public static boolean checkLeapYear(int year)
  15. {
  16. boolean a = (year % 4) == 0;
  17. boolean b = (year % 100) != 0;
  18. boolean c = ((year % 100 == 0) && (year % 400 == 0));
  19.  
  20. return a && (b || c);
  21. }
  22. }

Result

java techstudy.org

     





Related Topics




Popular Posts