How should I create a batch file program that if I input a year the program will identify if it is a leap year or not.
@echo off
cls
echo. LEAP YEAR
echo. Enter Year:
exit /b 0
Well, its just that i need help like suggestions to what should i do.
It does not check if the input is correct:
Accordingly to Wikipedia a year is leap if it is divisible by 4 excepting if it is also divisible by 100, in which case it is leap only if it is also divisible by 400 ("divisible" means that the remainder of the division by the given number is zero). This way, 2000 and 2400 are leap years because their remainders when they are divided by 400 are zero, but 2100, 2200 and 2300 are not: these are special cases because their remainders when they are divided by 100 are zero.
In
set /A
command the!
boolean NOT operator gives 1 if its operand is zero and gives 0 in any other case, soset /A "leap=!(year%%4)"
gives 1 if the year is divisible by 4 and zero in any other case; this gives the first part of the result.After that we need to subtract 1 from this value in years 2100, 2200 and 2300, but subtract nothing in years 2000 and 2400; that is:
If the year is not divisible by 100 then both
a
andb
values are equal to 1, soa-b
is zero and the result is given just by the original remainder by 4.This way, the formula
set /A "leap=!(year%%4) + (!!(year%%100)-!!(year%%400))"
gives the complete result.You could abuse PowerShell's date constructor. If it errors attempting to set the date object to February 29 of nnnn, then it's not a leap year.
You can do the same with a JScript hybrid. It's a little more code, but it's much faster.