Megatron11
Megatron11

Reputation: 39

Create a program in Java wherein if a year is a leap year will return 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 that how to return true if it's a leap year and false if it's not a leap year.

Upvotes: 3

Views: 1575

Answers (4)

almightyGOSU
almightyGOSU

Reputation: 3739

Just do this:

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

Pass in the year you wish to check, and the function will return you true/false based on whether that is a leap year.

Note: I am assuming the program just needs to contain a method that is able to return true/false based on the condition mentioned in the question, and not a program that returns true/false (which cannot be done).

Upvotes: 1

Stephen C
Stephen C

Reputation: 718698

Create a program in java wherein if a year is a leap year will return true otherwise false

A program cannot return true. A program does not return anything in the Java sense. It can set a return code, but that is a number, not true or false.

(The way to set a return code is to use System.exit(int) ... where the parameter is the return code. The code is typically truncated to a value between -128 and +127 ... though that is OS dependent.)

A method can return true, but a void method cannot return anything.

If you want to declare a method that can return true or false, then you need to declare the return type to be boolean; e.g.

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

Upvotes: 5

Deb
Deb

Reputation: 2972

You can create a method that will judge whether a year is 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) */
    }

}

Upvotes: 2

Emil Laine
Emil Laine

Reputation: 42828

To be able to return true or false you need a method that's declared to return a 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 that 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");

Upvotes: 0

Related Questions