R Program to Check for Leap Year

A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400.


Example: Check Leap Year

# Program to check if
# the input year is
# a leap year or not

year = as.integer(readline(prompt="Enter a year: "))
if((year %% 4) == 0) {
    if((year %% 100) == 0) {
        if((year %% 400) == 0) {
            print(paste(year,"is a leap year"))
        } else {
            print(paste(year,"is not a leap year"))
        }
    } else {
        print(paste(year,"is a leap year"))
    }
} else {
    print(paste(year,"is not a leap year"))
}

Output 1

Enter a year: 1900
[1] "1900 is not a leap year"

Output 2

Enter a year: 2000
[1] "2000 is a leap year"

If a year is divisible by 4, 100 and 400, it's a leap year.

If a year is divisible by 4 and 100 but not divisible by 400, it's not a leap year.

If a year is divisible by 4 but not divisible by 100, it's a leap year.

If a year is not divisible by 1, it's not a leap year.

This logic is implemented in the above program using nested if...else statement.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges