Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Leap Year Checker
Overview
For example, let’s say you want to create a program that checks whether a given year’s a leap year or not. It’s a leap year if the year’s evenly divisible by 4. But if it’s evenly divisible by 100, then it also needs to be evenly divisible by 400. The Figure shows you a flowchart of what you’d need to test for.
Figure: A flowchart for checking if a given year’s a leap year
Here are three examples of these rules:
- Year 2020 is divisible by 4. Since it isn’t evenly divisible by 100, it’s a leap year!
- Year 2100 is divisible by 4. Because it’s evenly divisible by 100, you need to check its divisibility by 400. Since it isn’t evenly divisible by 400, it’s not a leap year.
- Year 2400 is divisible by 4. Because it’s evenly divisible by 100, you’ll need to check its divisibility by 400. Since it’s evenly divisible by 400, it’s a leap year!
Assignment
Create a program that implements this flowchart (see code below). Also, you’ll find the output from three sample runs.
Code
1 ' Determines if a given year is a leap year
2 TextWindow.Write( "Enter a year (like 2050): ")
3 year = TextWindow.ReadNumber()
4 If ( Math.Remainder( year,4) <> 0 ) Then ' Not divisible by 4
5 TextWindow.WriteLine(year + " isn't a leap year.")
6 Else ' divisible by 4
7 If ( Math.Remainder( year,100) <> 0 ) Then ' Not divisible by 100
8 TextWindow.WriteLine( year + " is a leap year.")
9 Else ' divisible by 4 and 100
10 If ( Math.Remainder( year,400) = 0 ) Then ' Divisible by 400
11 TextWindow.WriteLine( year + " is a leap year.")
12 Else ' Divisible by 4 and 100, but not 400
13 TextWindow.WriteLine( year + " isn't a leap year.")
14 EndIf
15 EndIf
16 EndIf
Output Examples
Enter a year (like 2050): 2020
2020 is a leap year.
Enter a year (like 2050): 2100
2100 isn’t a leap year.
Enter a year (like 2050): 2400
2400 is a leap year.
Explanation
After getting the year from your user, Small Basic checks if the year’s divisible by 4 (Line 7). If not, then your program runs the statement on Line 8 and ends.
If the input year’s divisible by 4, your program moves on to Line 10. If the year isn’t evenly divisible by 100, then it’s a leap year; the program runs the statement on Line 11 and then ends.
If the entered year’s evenly divisible by both 4 and 100, then your program runs Line 13, which divides the year by 400. If the input year’s divisible by 400, it’s a leap year; your program runs the statement on Line 14 and then ends.
If it isn’t divisible by 400, then the input year isn’t a leap year; your program runs the statement on Line 16 and then ends.
Learn more about Small Basic here:
https://blogs.msdn.com/smallbasic
Enjoy learning how to program with Small Basic, and have a Small and Basic week!
- User Ed & Majed Marji
Comments
Anonymous
March 17, 2015
I formatted the code and output. Added signature.Anonymous
March 18, 2015
Added a link to the Small Basic Blog.Anonymous
December 14, 2015
Going to turn this into a Wiki article: social.technet.microsoft.com/.../32923.small-basic-sample-leap-year-checker.aspxAnonymous
December 28, 2015
UPDATE: Code formattingAnonymous
December 29, 2015
And here's the Small Basic blog post that features the Wiki article: blogs.msdn.com/.../featured-small-basic-article-leap-year-checker.aspxAnonymous
January 31, 2016
Computers Today (part 1 of 6) blogs.msdn.com/.../computers-today.aspx ..... CS SPOTLIGHT: Girls in computer programming... why it matters!!! blogs.msdn.com/.../cs-spotlight-girls-in-computer-programming-why-it-matters.aspx ... Computational Thinking - Videos & Papers by Jeannette Wing blogs.msdn.com/.../computational-thinking-videos-amp-papers-by-jeannette-wing.aspx