This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
Idiom #272 Play FizzBuzz
Fizz buzz is a children's counting game, and a trivial programming task used to affirm that a programmer knows the basics of a language: loops, conditions and I/O.
The typical fizz buzz game is to count from 1 to 100, saying each number in turn. When the number is divisible by 3, instead say "Fizz". When the number is divisible by 5, instead say "Buzz". When the number is divisible by both 3 and 5, say "FizzBuzz"
- C++
- C++
- C#
- Dart
- Elixir
- Fortran
- Go
- JS
- Java
- Lisp
- Lua
- Lua
- PHP
- Pascal
- Pascal
- Perl
- Python
- Python
- Python
- Python
- Python
- Ruby
- Rust
- Smalltalk
string FizzBuzzOrNumber(int number) => number switch {
var n when n % 15 == 0 => "FizzBuzz",
var n when n % 3 == 0 => "Fizz",
var n when n % 5 == 0 => "Buzz",
var n => n.ToString()
};
for (var i = 1; i <= 100; i++)
{
Console.WriteLine(FizzBuzzOrNumber(i));
}
Using C# 9, we can use top level statements to avoid the need for a namespace, class and main method.
integer :: i
logical :: fizz, buzz
do i=1,100
fizz = mod(i,3) == 0
buzz = mod(i,5) == 0
if (fizz) write (*,'(A)',advance="no") 'Fizz'
if (buzz) write (*,'(A)',advance="no") 'Buzz'
if (.not. fizz .and. .not. buzz) write (*,'(I0)',advance="no") i
write (*,'(A)',advance="no") ', '
end do
write (*,'()')
end program
Using non-advancing I/O to avoid starting on the next line.
for n:=1; n<=100; n++ {
out:=""
if n%3==0 {
out=out+"Fizz"
}
if n%5==0 {
out=out+"Buzz"
}
if out=="" {
out=out+strconv.Itoa(n)
}
fmt.Println(out)
}
for (let i = 1; i <= 100; i++) {
let out = "";
if (!(i % 3)) out += "Fizz";
if (!(i % 5)) out += "Buzz";
if (!out) out = i;
console.log(out);
}
!(i % 3) and !(i % 5) are similar to (i % 3) == 0 and (i % 5) == 0, as !0 evaluates to true.
program fizzbuzz;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
var i: Integer;
begin
for i := 1 to 100 do
begin
if (i mod 3 = 0) and (i mod 5 = 0) then WriteLn(IntToStr(i) + ' = FizzBuzz')
else if (i mod 3 = 0) then WriteLn(IntToStr(i) + ' = Fizz')
else if (i mod 5 = 0) then WriteLn(IntToStr(i) + ' = Buzz')
else WriteLn(IntToStr(i));
end;
readln;
end.
for i in range(1, 100+1):
out = ""
if i % 3 == 0:
out += "Fizz"
if i % 5 == 0:
out += "Buzz"
print(out or i)
n=1
while(n<=100):
out=""
if(n%3==0):
out=out+"Fizz"
if(n%5==0):
out=out+"Buzz"
if(out==""):
out=out+str(n)
print(out)
n=n+1
for i in 1..101 {
match i {
i if (i % 15) == 0 => println!("FizzBuzz"),
i if (i % 3) == 0 => println!("Fizz"),
i if (i % 5) == 0 => println!("Buzz"),
_ => println!("{i}"),
}
}