Scala Program to Check a Leap Year

Posted in

Sangeeta Gulia
Last updated on January 13, 2023

    According to Nasa Science , the earth requires approximately 365.242375 days to orbit around the sun. This entire duration in which the earth orbits around the sun is referred to as a year. We usually round off the days from 365.242375 to 365 to keep it simple. To cover the decimal digit (approx 1/4th of the day), we add one day to our calendar approximately every four years. And the year which has this additional day is known as Leap Year. This additional day is 29th February.

    Isn’t it interesting to write a program that checks whether a particular year is a leap or not? Of course, it is. So, I will help you write a Scala program to achieve the same.

    Algorithm to Identify If a Number is a Leap Year

    This is a bit tricky to identify if a number is a leap year or not. You can understand and learn it with the help of the following flowchart:

    Leap Year Algorithm

    To reiterate the above diagram in words, a year is a leap year if it satisfies either of the two below two conditions:

    • The year should be the multiple of 400.
    • The year should be the multiple of 4 but of 100.

    Understanding Algorithm Rules

    Here are a few steps that help you identify whether a year is a leap or not.

    1. If a year is evenly divisible by four, go to step 2. Otherwise, go to step 5.
    2. If a year is evenly divisible by 100, go to step 4. Otherwise, go to step 3.
    3. Check if the number is evenly divisible by 400. If yes, go to step 4; else, go to step 5.
    4. The year is a leap year.
    5. The year is not a leap year.

    Scala Program to Check a Leap Year

    objectLeapYearextendsApp {
    println("Please enter an integer number/year")
    val year =
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
    println(s"${year} is a leap year")
    } else {
    println(s"${year} is not a leap year")


    Sample Output 1:

    Please enter an integer number/year
    2000 is a leap year

    Sample Output 2:

    Please enter an integer number/year
    2100 is not a leap year


    We have already covered the algorithmic details and its understanding for checking if a year is a leap year or not. One key programming aspect of this program lies within the if condition.

    Here, we have 3 sub-conditions to check, two of the conditions are grouped by the AND operator to identify if a year is a leap year or not, and the 3rd condition itself is the deciding factor and does not an additional condition. You can see in the above program how I have placed all three conditions.


    Do try out this program with some other input examples and identify what all years fall under leap year. When you try on your own, you can understand the concept better.

    Happy Coding!

    Leave a Comment on this Post