# Determine a leap year

7 posts
• Page

**1**of**1**### Determine a leap year

I'm useless at maths, could somebody help?

I need to check if a year is a leap year, within a BASIC program. It's actually Liberty BASIC v4.01 but the syntax is more or less standard BASIC. In fact a coded example is probably not neccessary, I just need to know mathematically how to do it.

Thanks for any help with this. My brain hurts after a lot of coding today & my mind has gone completely blank.

There's some VB code at http://www.freevbcode.com/ShowCode.asp?ID=391 but I'm not sure what it's on about, can anyone tell me what it is doing (just the guts that do the actual determination of leap year/not leap year?)

[edit]

Eh up. here's a QBASIC example which is closer to Liberty BASIC's syntax: (Liberty is basically QBASIC with a GUI)

- Code: Select all
`C. LEAP YEAR:`

------------

To determine if a year is leap year, is actually a simple matter.

The rule is as folows:

If the year is evenly divisible by 4

and not divisible 100,

or if the year is evenly divisible by 400,

then it's a leap year.

Sample usage of ISLEAPYEAR function below:

Assume we looked up the maximum number of days per month in a table.

Now, if the month is February and the year is a leap year,

we want to add 1 to the maximum days per month.

IF MM=2 AND ISLEAPYEAR(YYYY) THEN MAXDAYS=MAXDAYS+1

The following is the function:

Note: It returns -1 when true (leap year), or 0 if false.

FUNCTION IsLeapYear (Z) STATIC

IsLeapYear = (Z MOD 4 = 0 AND Z MOD 100 <> 0) OR (Z MOD 400 = 0)

END FUNCTION

I guess my question boils down to, what does:

- Code: Select all
`(Z MOD 4 = 0 AND Z MOD 100 <> 0) OR (Z MOD 400 = 0)`

...mean in english ? (can someone explain the math terms..)

..guess this further boils down to, what is the MOD term?

There doesn't seem to be a MOD operator in Liberty BASIC

These are the math functions I have available in Liberty:

Arithmetic

Arithmetic operators are as follows:

+ addition

- subtraction

* multiplication

/ division

^ power

Examples:

print 2 + 3 'addition

print 6 - 2 'subtraction

print 4 * 7 'multiplication

print 9 / 3 'division

print 2 ^ 3 'power - (two to the third power)

print (4+2)*6^2 'multiple expressions are evaluated according to the following rules of order:

Order

Expressions are evaluated in this order:

( ) expressions within parentheses are evaluated first

^ exponents are evaluated next

* / multiplication and division are evaluated next

+ - addition and subtraction are evaluated last

See also:

SIN

COS

TAN

ASN

ACS

ATN

ABS

EXP

LOG

HEXDEC

DECHEX$

INT

MAX

MIN

RND

SQR

VAL

STR$

USING

Found some info here http://www.asp101.com/articles/steven/mod/default.asp about 'MOD' (??) but could still do with some help putting it all together, if someone would know how to do it

I wonder if I could use Liberty's INT function:

Description:

This function removes the fractional part of "n" (a number), leaving only the whole number part behind. The fractional part is the part of the number after the decimal point.

Usage:

[retry]

input "Enter an integer number>"; i

if i<>int(i) then bell: print i; " isn't an integer! Re-enter.": goto [retry]

i.e. to check a year is 'evenly divisible', could I do the calculation, then compare the result with the "INT'ed" result, and if they're the same, it has been an 'even' division.. oh I dunno, am I making it too complicated.

Any math experts your opinion appreciated

- marathonman

I think it would be pretty easy if your follow these rules.

'True if it is divisible by 4

'False if it is divisible by 100

'TRUE if it is divisble by 400

I'll try to come up with something for this.

ZeroByte

- ZeroByte
**Posts:**1199**Joined:**Thu Aug 01, 2002 2:12 am**Location:**Mexico, NY**Real Name:**Mike Sheats

Does this look valid? Like, assuming the syntax is okay, it would give an accurate answer..

- Code: Select all
`Function IsLeapYear(Z)`

IsLeapYear = ((mod(Z, 4) = 0) AND (mod(Z, 100) <> 0)) OR (mod(Z, 400) = 0)

End Function

function mod(a,b)

mod = (a/b - int(a/b)) * b

end function

- marathonman

i haven't even looked at basic in years but as for the programming and math of that, it looks like it should work, ... good implementation of the modulus function, ...but why multiply by "b" ?

- poisonbl
**Posts:**432**Joined:**Mon Nov 15, 2004 10:02 pm**Location:**WVU -- Morgantown, WV. -- USA (TZ: -5 hrs GMT, -4 DST)

Okay its been 20 years since I programmed in BASIC but... I imagine something like this would work (I cant remember how to make it sort whole numbers (INT?) So I did it the long way

Well dont know if it helps but it was fun!

Oh BTW thats not liberty BASIC I never heard of that...

Thats basic BASIC

- Code: Select all
`10 input "Please type year.";a$`

20 a$/4=b$

30 for c = 1 to 1000

40 if c=B$ then goto 70

50 next c

70 if b$=100 then goto 100

71 ib b$=200 then goto 100

72 if b$=300 then goto 100

73 if b$=400 then goto 100

74 if b$=500 then goto 100

75 if b$=600 then goto 100

76 ib b$=700 then goto 100

77 if b$=800 then goto 100

78 if b$=900 then goto 100

79 if b$=1000 then goto 100

80 print "This is a leap year."

90 Goto 110

100 print "This is not a leap year."

110 END

Well dont know if it helps but it was fun!

Oh BTW thats not liberty BASIC I never heard of that...

Thats basic BASIC

- Neuromancer
**Posts:**5756**Joined:**Sun Mar 28, 2004 5:19 am**Location:**West Virginia

why multiply by "b" ?

I don't know. I found it on a webpage. You're right, it shouldn't need it should it.. not simply to determine if mod is zero.. maybe non-zero mod values are mathematically incorrect without it. This is where my maths goes down the pan, I can count up to ten ok but further than that it's a struggle lol

Wow Neuro.. line numbers? they're antiques now aren't they? I remember feeling a bit lost at the point when BASIC moved away from them.. that was the point at which I entered a 10-year exile from the computer world, until the 'net started to become popular, whereupon I came back into the fold (and had to spend the next 5 years learning how to program all over again - scary how times change so fast)

Line numbers were great you can still use them in most modern BASICS I think, but it seems to be discouraged, certainly uncommon in the code I've seen recently. But if it does the job all's good!

- marathonman

marathonman wrote:Wow Neuro.. line numbers? they're antiques now aren't they? I remember feeling a bit lost at the point when BASIC moved away from them.. that was the point at which I entered a 10-year exile from the computer world, until the 'net started to become popular, whereupon I came back into the fold (and had to spend the next 5 years learning how to program all over again - scary how times change so fast)

Line numbers were great you can still use them in most modern BASICS I think, but it seems to be discouraged, certainly uncommon in the code I've seen recently. But if it does the job all's good!

Ahh I told you I had not used BASIC in 20 years

I have worked with vbs scripts a little bit, but it was mostly cut n paste and VERY little editing..

I will write a little DOS here and there, but programming isn't really my thing.

Is web design considered programming? I knew very little HTML and now am trying to find info on CSS, XHTML, PHP and eventually XML... my new site is incorporating a few of these and though I have GUI methods to take care of everything I always liked doing things the hardway. Got me in trouble a lot in school, when ever a teacher would tell me something I would ask why. And they would get really pissed if they did not know. "You just have to accept it dammit!"

LOL

- Neuromancer
**Posts:**5756**Joined:**Sun Mar 28, 2004 5:19 am**Location:**West Virginia

7 posts
• Page

**1**of**1**Return to HTML, CSS, and Scripts

### Who is online

Users browsing this forum: No registered users and 4 guests