Saturday, May 22, 2010

In c++ how can you figure out?

in c++ how should you write in visual studio that if it is a a leap year the computer will display leap year or else it will display it is not a leap year for example if i put 1992 i want the computer to tell me it is not a leap year rather 1994 is a leap year

In c++ how can you figure out?
Here is a quick algorhythm





bool leap;


int year = 1994;





if (!(year % 4))


if (!(year % 100)) {


if (!(year % 400)) {


leap = TRUE;


} else {


leap = FALSE;


}


} else {


leap = TRUE;


}


} else {


leap = FALSE;


}





Basically, it is a leap year if the year is divisible by 4, except if it's divisble by 100 when it's not, except if it's divisible by 400, when it is.





So, 1600 was a leap year, but 1700 and 1800 wasn't, but 2000 was.
Reply:Years evenly divisible by 4, but not by 100 are leap years (unless they're evenly divisible by 400).
Reply:Is this it?





Function IsLeapYear(ByVal SomeValue As Variant) As Boolean


On Error GoTo LocalError


Dim intYear As Integer


'The trick here is make sure that we get an integer


'The 3 Golden rules are:


'True if it is divisible by 4


'False if it is divisible by 100


'TRUE if it is divisble by 400


If IsDate(SomeValue) Then


intYear = CInt(Year(SomeValue))


Else


'try and get an integer from the parse


'does not matter if we get an error


'because the error trap will catch it


intYear = CInt(SomeValue)


End If


If TypeName(intYear) = "Integer" Then


IsLeapYear = ((intYear Mod 4 = 0) And _


(intYear Mod 100 %26lt;%26gt; 0) Or (intYear Mod 400 = 0))


End If


Exit Function


LocalError:


IsLeapYear = False


End Function





WAIT - I'm a dork - that's VB. Hang tight I'm looking...


No comments:

Post a Comment