Java Program to Check Leap Year

Configurare noua (How To)

Situatie

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.

Solutie

Pasi de urmat

public class Main {

public static void main(String[] args) {

// year to be checked
int year = 1996;
boolean leap = false;

// if the year is divided by 4
if (year % 4 == 0) {

// if the year is century
if (year % 100 == 0) {

// if year is divided by 400
// then it is a leap year
if (year % 400 == 0)
leap = true;
else
leap = false;
}

// if the year is not century
else
leap = true;
}

else
leap = false;

if (leap)
System.out.println(year + ” is a leap year.”);
else
System.out.println(year + ” is not a leap year.”);
}
}

Output

1900 is not a leap year.

Tip solutie

Permanent

Voteaza

(2 din 5 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?