Small Basic Sample: 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
**
Code Block Version:
**
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://techcommunity.microsoft.com/t5/small-basic-blog/bg-p/SmallBasic
See Also
- Small Basic Reference Documentation: Math Object
- Small Basic FAQ
- Download Microsoft Small Basic
- Wiki: Small Basic Portal
- Small Basic Samples