[ Prev | Index | Next ]
Japanese English
Now Loading...
Download
Please download on PC (not smartphone) and extract the ZIP file. Then double-click VCSSL.bat (batch file) to execute for Microsoft® Windows®, or execute VCSSL.jar (JAR file) on the command line for Linux® and other OSes.
For details, see How to Use.

Fizz Buzz Program

A program printing the correct result of Fizz Buzz game.

This program is written in Vnano. In addition, programs written in Java, C, ann C++ are also distributed in Code section.

スポンサーリンク


How to Use

Download and Extract

At first, click the "Download" button at the above of the title of this page by your PC (not smartphone). A ZIP file will be downloaded.

If you are using Windows, right-click the ZIP file and choose "Properties" from the menu, and enable "Unblock" checkbox at the right-bottom (in the line of "Security") of the properties-window. Otherwise, when you extract the ZIP file or when you execute the software, security warning messages may pop up and the extraction/execution may fail.

Then, please extract the ZIP file. On general environment (Windows®, major Linux distributions, etc.), you can extract the ZIP file by selecting "Extract All" and so on from right-clicking menu.

» If the extraction of the downloaded ZIP file is stopped with security warning messages...

Execute this Program

Next, open the extracted folder and execute this VCSSL program.

For Windows

Double-click the batch file "VCSSL.bat" to execute.

For Linux, etc.

Execute "VCSSL.jar" on the command-line terminal as follows:

cd <extracted_folder>
java -jar VCSSL.jar

» If the error message about non-availability of "java" command is output...

Result

Then an window will be launched, and the result of Fizz Buzz game from 1 to 100 will be printed on it:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
...

If you want to change the number of lines to be printed, open the program "FizzBuzz.vnano" by any text editor, and modify the following value:

About Fizz Buzz

What is Fizz Buzz Game?

Originally, Fizz Buzz is a game for humans. The rule of the game is as follows:

  • Count up numbers, like as: 1, 2, ...
  • When the number is a multiple of 3, say "Fizz" instead of the number.
  • When the number is a multiple of 5, say "Buzz" instead of the number.
  • When the number is a multiple of both 3 and 5, say "Fizz Buzz" instead of the number.

Reference: The page of "Fizz buzz" in Wikipedia

As a Programming Task

Fizz Buzz is also well-known as an exercise/test for beginners of programming, like the following:

Task: Implement a program printing the correct result of Fizz Buzz.

Probably, it is because, it requires to understand/use a loop and conditional branches correctly.

In addition, even if one understand behaviour of if/else/for statements correctly, to use them in combination for practical task requires exercise.

And Fizz Buzz is just right for exercise at that level.

So, if you come to this page by searching how to implement a program of Fizz Buzz, I recommend you to try to implement it by yourself at least once, before reading the next "Code" section. It may be little difficult, but it should be a very good exercise.

For Code Golf

If you can implement a Fizz Buzz program easily, Let's try to do an additional task as a game:

Task: Coding a Fizz Buzz program as short as possible.

The person who wrote shortest code is the winner. This kind of game is called as "code golf". Fizz Buzz is also a good exercise for code golf.

If all players are high-level code golfers, we can tune the difficulty level by additional regulations, such as: "Don't use remainder (%) operators", "Don't use conditional branches", and so on.

I'll write my short code in Java, at the end of the next "Code" section. Let's compete!

Code

In this section, Let's read the actual code of a Fizz Buzz program.

Whole Code

The following is the whole code, where the code is written in Vnano. Vnano has simple C-like syntax, so if you are accustomed to C or other languages having C-like syntax, you can read the Vnano code very easily. Also, you can download code written in C/C++/Java, from the right-click menu of the following links.

Let't see details of parts in code.

The First Line

The fist line "coding UTF-8;" is not important. It is for preventing so-called "mojibake" by declaring the character set of the file, especially when you use non-ascii characters in code.

After the Line 4: Declaring a Variable "max"

Next is a line declaring a variable "max", and storing the maximum (ending) number of the counting in the Fizz Buzz game.

This variable be referred by the "for" statement in code, as explained next.

After the line 6: A Loop for Counting Up Numbers

This part is a very typical loop using "for" statement. It counts up numbers from 1 to 100, and execute code in {...} repeatedly, for each number. In {...}, you can refer that number as the value of the counter variable: i.

If you want to understand behaviour of this part clearly, replace code in {...} by { println(i); }. Then the program become to print the value of i simply, for each cycle:

1
2
3
...
100

From here, read code and explanations, as if you are just implementing conditional branches into the above {...}.

After the line 9: A Branch for When "i" is a Multiple of Both 3 and 5

Next, is the first branch in {...} of the above "for" loop:

Code in {...} of the above "if" statement will be executed when the condition "i%3 == 0 && i%5 == 0" is satisfied. Let's focus on details of this conditional expression.

The symbol "%" is the "remainder" operator. The value of "m%n" is the remainder of the division of m over n. For example, 10%2 is 0, 10%3 is 1, and 10%7 is 3. In general, the value of m%n is 0 when m is a multiple of n.

The symbol "&&" is the "logical AND" operator. The condition "c1 && c2" is satisfied when both c1 and c2 are satisfied. In other word, "c1 && c2" isn't satisfied when at least one of c1 and c2 isn't satisfied.

Hence, the condition "i%3 == 0 && i%5 == 0" is satisfied when the value of "i" is a multiple of both 3 and 5, and then {...} of this if statement will be executed. It prints a line "FizzBuzz". Otherwise it will be skipped.

After the line 13: A Branch for When "i" is a Multiple of Only 3

Next:

This part looks like more simple than the previous part. However, beware of overlooking a very important word: "else".

By putting this "else" before an "if" statement, it become to be skipped when the condition of the previous (upper) "if" statement is satisfied.

Hence, this combination of "else" and "if" (so-called "else if") prints a line "Fizz" when "i" is a multiple of 3, but it will be skipped when "i" is a multiple of both 3 and 5 (because it is handled by our previous "if" statement).

After the line 19: A Branch for When "i" is a Multiple of Only 5

Next part is almost the same as the previous part:

This part will be skipped when conditions of any of previous "if" and "else if" is satisfied.

Hence, this parts println a line "Buzz" when "i" is a multiple of only 5.

After the line 24: The Process for When All Conditions Aren't Satisfied

Next is the last part:

This part begins with only "else". It hasn't "if" and a condition expression. So, when all conditions of previous "if" and "else if" aren't satisfied, this part will always be processed.

As the result, this part prints the value of "i" when it isn't a multiple of any of 3 and 5.

Finished

That's all.

As shown here, the combination of:

if(...) {
    ...
}
else if (...){
    ...
}
else if (...){
    ...
}
else {
    ...
}

is used very frequently in general programs. So Fizzbuzz is a very good exercise to get used to the above pattern.

Also, my answer of the code golf is:

for(int i=0;i++<100;System.out.println(i%15==0?"FizzBuzz":i%3==0?"FIzz":i%5==0?"Buzz":i));

The above is written in Java. Copy & paste the above code into the main function of a Java source file, and compile & run it. Then you can get the same result as the code explained in this section.

License

The license of this VCSSL / Vnano code (the file with the extension ".vcssl" / ".vnano") is CC0 (Public Domain), so you can customize / divert / redistribute this VCSSL code freely.

Also, if example code written in C/C++/Java are displayed/distributed on Code section of this page, they also are distributed under CC0, unless otherwise noted.



Sponsored Link



Japanese English
[ Prev | Index | Next ]
Fizz Buzz Program

A program printing the correct result of Fizz Buzz game.
Vnano | Compute Integral Value Numerically

Example code computing integral values numerically by using rectangular method, trapezoidal method, and Simpson's rule.
Vnano | Output Data of Numerical Integration For Plotting Graph

Example code computing integrated values numerically, and output data for plotting the integrated functions into graphs.
Index
[ Prev | Index | Next ]
Fizz Buzz Program

A program printing the correct result of Fizz Buzz game.
Vnano | Compute Integral Value Numerically

Example code computing integral values numerically by using rectangular method, trapezoidal method, and Simpson's rule.
Vnano | Output Data of Numerical Integration For Plotting Graph

Example code computing integrated values numerically, and output data for plotting the integrated functions into graphs.
News

Opened the Website of Exevalator
2022/07/24 - We have opened the official website of "Exevalator", the open-source expression calculator library (for Java/C#/C++/Rust) we released in April.

The Internal Architecture of Exevalator
2022/05/04 - Explaining the internal architecture of Exevalator, which is an open-source expression calculator library we released last month.

Released "Exevalator": A Multilingual & Copyright-Free Expression Evaluator Library
2022/04/16 - We have released a new library "Exevalator" which enables you to calculate values of numerical expressions stored in string variables, on Java/C#/C++/Rust programs.

Announcement (Twitter)