Create a Java program where if the year is a leap year it returns true, otherwise false

This is my code

public class Leapyear{
  public static void main(String []args){
    for(int year =2000; year <=2020; year++){
      if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
        System.out.println("Year " + year + " is a leap year");
      else
        System.out.println("Year " + year + " is not a leap year");
}

      

but the question is, I don't know how to return true if it is a leap year and false if it is not a leap year.

+3


source to share


4 answers


You can create a method that will determine if the year is a leap year or not. Like this.



public class Leapyear{
    public static void main(String []args){
        for(int year =2000; year <=2020; year++){
            if(isLeapYear(year)) {
              System.out.println("Year " + year + " is a leap year");
            else
              System.out.println("Year " + year + " is not a leap year"); 
        }
     }

    public static boolean isLeapYear(int year) {
        return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)) /* It will return boolean value (true or false) */
    }

}

      

+2


source


Create a program in java that returns true if year is leap year, false otherwise

The program cannot return true

. The program does not return anything in the Java sense. It can set a return code, but it's a number, not true

or false

.

(The way to set the return code is to use System.exit(int)

... where the parameter is the return code. Usually the code is truncated to a value between -128 and +127 ... although this depends on the OS.)



The method can return true

, but the method void

cannot return anything.

If you want to declare a method that can return true

or false

, you need to declare a return type boolean

; eg

    public boolean isLeapYear(int year) { /* you fill in the rest */ }

      

+5


source


To be able to return true

or false

, you need a method that declares a return boolean

:

public static boolean isLeapYear(int year) {
    if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
        return true;
    else
        return false;
}

      

And then call this method inside main

:

for (int year = 2000; year <= 2020; year++) {
    if (isLeapYear(year))
        System.out.println("Year " + year + " is a leap year");
    else
        System.out.println("Year " + year + " is not a leap year");

      

0


source


Just do the following:

public boolean isLeapYear(int year) {
    return ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)));
}

      

Go to the year you want to check and the function will return true / false for you depending on whether it is a leap year.

Note: I am assuming that the program should just contain a method that can return true / false based on the condition mentioned in the question, not a program that returns true / false (which cannot be executed).

0


source







All Articles