A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Fizz Buzz in Befunge page! Here, you'll find the source code for this program as well as a description of how the program works.
1 > :3% v v \0 _ "zziF",,,,v v \1 < > :5% v v _v#\ _ "zzuB",,,,v v >:. v $\ < v < @ > :25* ::, *1-` | ^ +1 <
Fizz Buzz in Befunge was written by:
If you see anything you'd like to change or update, please consider contributing.
Befunge, like many other esoteric languages, has a very unique look to it, and is very complex at first glance. Each operation is denoted by a single character, and the file can be traversed in four directions. If you have not read about it before, do that first, starting here: The Befunge Programming Language.
The Fizz-Buzz problem itself is here:
If a number is divisible by 3, print the word 'Fizz' instead of the number. If the number is divisible by 5, print the word 'Buzz' instead of the number. Finally, if the number is divisible by both 3 and 5, print 'FizzBuzz' instead of the number. Otherwise, just print the number.
Don't worry if the code looks confusing, we will break it down and go over the different sections.
The first section to look at will handle the Fizz statement, and adds a boolean value to the stack for use later.
1 > :3% v v \0 _ "zziF",,,,v v \1 <
The program starts at the top left corner, meaning that the stack will start with a 1, and then cross over the top line, running the characters
:will make a copy of the top value, which will count the current iteration.
3will push a 3 on top of the stack.
%will run modulus on the top two values, returning the remainder of x / 3.
The pointer then moves down onto the
_, which will run as an if statement, moving right if the top or the stack is 0, and left if it is anything else. Combined with the previous statements, it will move right whenever the initial value is divisible by 3, pushing
zziF to the stack and printing it out in the proper order.
In both cases, the instruction pointer then returns to the left, adding either a 1 to the stack after printing Fizz, or a 0 if nothing was printed. Each
\ then swaps the top two values, putting this indicator value below the current run count.
The second section of code is very similar, intended to handle the Buzz part of the program.
> :5% v v _v#\ _ "zzuB",,,,v v >:. v $\ < v <
Like the previous section,
:5% will calculate the remainder of x / 5, and the
_ will send the pointer left or right accordingly.
Buzzand head to the downward arrow at the center,
\$deleting the 0 or 1 value that was stored earlier.
\will switch that number with the binary value stored in the first set, and
#will jump straight to the
_statement at the start of the second line. This checks the previous boolean, and in the case that nothing has been printed out so far, directs the instruction pointer to
:., printing out the run count as a numerical value.
The final section handles the new line and sets up the next loop of the program.
v @ > :25* ::, *1-` | ^ +1 <
The middle line has three main statements.
:25* copies the current value and pushes 2 * 5 to the stack.
::, makes 2 more copies of 10, then prints out one of them as a newline. Finally,
*1-' multiplies the 10s together, subtracts 1 from it to get 99, then pushes 1 if the current iteration is larger.
At the end,
| checks for a zero on top of the stack, sending the pointer downward if it is found. If there is a 1 and the current iteration is > 99, then the
@ symbol is activated instead, ending the program.
If the counter has not yet reached 100,
1+ adds 1 to it, and the pointer is sent back to the beginning of the loop.
Because of the particular design of the language, it is recommended to use a Befunge interpreter. It is a lot easier to learn and play around with when you can step through and see what the pointer is doing.
If you do want to use a compiler, here is another option: