Q:

Kotlin program to check leap year

belongs to collection: Kotlin Basic Programs

0

What is a Leap Year?

"A leap year is a calendar year that contains an additional day added to keep the calendar year synchronized with the astronomical year or seasonal year." ~ Wikipedia

A Non-Leap year contains 365 days in a year while a Leap Year contains 366 days in a year.

In terms of programming logic, a leap year,

  1. should be divisible by 400 (In case of century years) .
  2. and, should be divisible by 4 (In case of non-century years).

Given a year, we have to check whether it is a leap year or not.

Example:

    Input:
    2020
    Output:
    2020 is Leap Year

    Input:
    2021
    Output:
    2021 is Not a Leap Year

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to check leap year in Kotlin

package com.includehelp.basic

import java.util.*

// Main Method Entry Point of Program
fun main(args: Array<String>) {
	// InputStream to get Input
	var reader = Scanner(System.`in`)

	//Input Integer Value
	println("Enter Year : ")
	var year = reader.nextInt();

	// checking leap year condition
	val isleap = if (year % 4 == 0){
		if (year % 100 == 0) {
			// Century Year must be divisible by 400 for Leap year
			year % 400 == 0
		} else true
	} else false;

	println("$year is ${if (isleap) "Leap Year" else "Not a Leap Year"} ")
}

Output

Run 1:
Enter Year :
2020
2020 is Leap Year
-------
Run 2:
Enter Year :
1987
1987 is Not a Leap Year
----
Run 3:
Enter Year :
2012
2012 is Leap Year
----
Run 4:
Enter Year :
1900
1900 is Not a Leap Year

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Kotlin Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Kotlin program to print the multiplication table o... >>
<< Kotlin program to check whether given number is po...