Monday, December 13, 2010

Test Question 1 Review 5 - final VB.NET and C# code

NOTE: there are many variations to solving this problem, provided they all work. This is just one way that meets the specification, and met my own criteria of not wanting to spend more than 10 minutes thinking about it. If you have other ways to do the same thing, that's great. Try it with test data of

startDate: 1495
endDate: 1605

which should show leap years every 4 years (starting with 1496 and ending with 1604) with the exception of 1500 (century non-leap) but includes 1600 (century leap).

VB.NET code:

Module Module1

Sub Main()
Console.WriteLine("please enter a start year for the leap year range calculator")
Dim startYear As Integer = CInt(Console.ReadLine())
Console.WriteLine("please enter an end year for the leap year range calculator")
Dim finishYear As Integer = CInt(Console.ReadLine())
Dim foundNotCenturyLeap As Boolean = False
Dim notCenturyLeap As String = ""

Console.WriteLine("The following years are leap years:")
For thisYear As Integer = startYear To finishYear
If (thisYear Mod 100 = 0) Then
If (thisYear Mod 400 = 0) Then
' leap year Century year
Console.WriteLine(thisYear)
Else
notCenturyLeap &= thisYear & ", "
foundNotCenturyLeap = True
End If
Else
If (thisYear Mod 4 = 0) Then
Console.WriteLine(thisYear)
End If
End If
Next
If foundNotCenturyLeap Then
Console.WriteLine("The following century years that fall within the date range and are NOT leap years are:")
Console.WriteLine(notCenturyLeap)
Else
Console.WriteLine("there are no century years that are not leap years within the range you have specified")
End If
Console.ReadLine()
End Sub

End Module


REVISION WORK:

See if you can re-write this VB.NET code into C#. You have a working prototype in another language and the pseudocode. Apart from some reference materials (hello Google), that should be all you would need. Put your answer up on the companion GoogleGroups site:

http://groups.google.com/group/pseudocodehelp