w3resource

Kotlin program: Check if a year is a leap year


Write a Kotlin program to check if a given year is a leap year.


Pre-Knowledge (Before you start!)

  • Basic Kotlin Syntax.
  • Kotlin Functions.
  • Conditional Statements.
  • Boolean Logic.
  • Modulo Operator.
  • Leap Year Concept.
  • String Interpolation.

Hints (Try before looking at the solution!)

  • Define the main() Function.
  • Create a Leap Year Function.
  • Use Boolean Logic.
  • Return a Boolean Value.
  • Call the Function.
  • Display the Output.
  • Test with Different Years.
  • Common Errors to Avoid:
    • Forgetting leap year rules for years divisible by 100 and 400.
    • Misplacing logic in Boolean expressions.
    • Using incorrect conditions.

Sample Solution:

Kotlin Code:

fun main() {
    val year = 2000
    //val year = 2003

       if (isLeapYear(year)) 
          {
            println("$year is a leap year.")
          } 
       else 
          {
            println("$year is not a leap year.")
          }
   }

fun isLeapYear(year: Int): Boolean {
    return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
}

Sample Output:

2003 is not a leap year.
2000 is a leap year.

Explanation:

In the above exercise,

  • Inside the "main()" function, a variable named year is declared and assigned a value.
  • The program then checks if the given year is a leap year by calling the isLeapYear() function and passing the year as an argument.
  • The result of the isLeapYear() function is evaluated in an if statement.
  • If the function returns true, the year is a leap year. The program prints the message "$year is a leap year." using string interpolation.
  • If the function returns false, the year is not a leap year. The program prints the message "$year is not a leap year." using string interpolation.

In the isLeapYear() function,

  • It takes an integer year as a parameter and returns a boolean value (true if it's a leap year, false otherwise).
  • Inside the function, a leap year is determined by the following conditions:
  • The year should be divisible by 4.
  • The year should not be divisible by 100, unless it is also divisible by 400.
  • The function uses the modulo operator % to check these conditions and returns the result as a boolean value.

Go to:


PREV : Find maximum and minimum of three numbers.
NEXT : Calculate circle area and perimeter.

Kotlin Editor:


Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.