xxxxxxxxxx
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
xxxxxxxxxx
public static void main(String[] args) {
int year = 2005;
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))){
System.out.println(year+" leap year");
}else {
System.out.println(year+" not a leap year");
}
}
xxxxxxxxxx
function isLeapYear(year){
if(year % 4 === 0 && year % 100 !== 0 || year % 400 === 0){
console.log('Leap Year')
}
else{
console.log('Not Leap Year')
}
}
isLeapYear(1988)
xxxxxxxxxx
// works in C, C++, C#, Java, JavaScript, and other C-like languages
// optimal technique
if (((year & 3) == 0 && ((year % 25) != 0) || (year & 15) == 0)) {
// leap year
}
// classic technique
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
// leap year
}
xxxxxxxxxx
$day = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
$year = 2021;
if (($year % 4 == 0 && $year % 100 != 0) || ($year % 400 == 0)) {
$day = [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
echo "This is leap year - get 366 day";
} else {
echo "This is NOT leap year - get ONLY 365 day";
}
leap year or not in java script
xxxxxxxxxx
var year= 1800;
if(((year%4) == 0 && ((year%100)!=0)) ||(year%400==0) ){
console.log("leap year");
}
else{
console.log("not a leap year");
}
xxxxxxxxxx
#include <stdio.h>
int main() {
int year;
year = 2016;
if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))
printf("%d is a leap year", year);
else
printf("%d is not a leap year", year);
return 0;
}
xxxxxxxxxx
year = int(input("Enter a year: "))
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
print(year, "is a leap year")
else:
print(year, "is not a leap year")
xxxxxxxxxx
import java.util.Scanner;
public class LeapYearChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = Integer.parseInt(scanner.nextLine());
String result = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? "is a leap year." : "is not a leap year.";
System.out.println(year + " " + result);
scanner.close();
}
}
xxxxxxxxxx
public class LeapYearChecker {
public static void main(String[] args) {
int year = 2024;
if (isLeapYear(year)) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
public static boolean isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return true;
} else {
return false;
}
}
}