Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

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"

#include <iostream>
#include <string>
for (int n=1; n<=100; n++) {
        string out="";
        if (n%3==0) {
            out=out+"Fizz";
        }
        if (n%5==0) {
            out=out+"Buzz";
        }
        if (out=="") {
            out=out+std::to_string(n);
        }
        cout << out << "\n";
    }
for(int i = 1; i <= 100; i++)
{
    if((i % 15) == 0) std::cout << "FizzBuzz" << std::endl;
    else if((i % 5) == 0) std::cout << "Buzz" << std::endl;
    else if((i % 3) == 0) std::cout << "Fizz" << std::endl;
    else std::cout << i << std::endl;
}
using System;
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));
}
for (int i = 1; i <= 100; i++) {
  var s = StringBuffer();
  if (i % 3 == 0) s.write("Fizz");
  if (i % 5 == 0) s.write("Buzz");
  if (s.isEmpty) s.write(i);
  print(s);
}
defmodule FizzBuzz do
  def run do
    for n <- 1..100 do
      case { rem(n, 3) == 0, rem(n, 5) == 0 } do
        { true, true } -> IO.puts("FizzBuzz")
        { true, false } -> IO.puts("Fizz")
        { false, true } -> IO.puts("Buzz")
        _ -> IO.puts(to_string(n))
      end
    end
  end
end

FizzBuzz.run
  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
import "fmt"
import "strconv"
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);
}
for (int i = 1; i <= 100; i++){
	if (i % 15 == 0)
	  System.out.println ("FizzBuzz");
	else if (i % 3 == 0)
	  System.out.println ("Fizz");
	else if (i % 5 == 0)
	  System.out.println ("Buzz");
	else
	  System.out.println (i);
      }
(loop for i from 1 to 100
   do (print
       (cond ((zerop (mod i 3))
              (if (zerop (mod i 5)) "FizzBuzz" "Fizz"))
             ((zerop (mod i 5)) "Buzz")
             (t (format nil "~d" i)))))
for n=1,100 do
 print(
  n%3==0
  and (n%5==0 and "FizzBuzz" or "Fizz")
  or (n%5==0 and "Buzz")
  or tostring(n)
 )
end
n=1
while(n<=100) do
    out=""
    if(n%3==0) then
        out=out .. "Fizz"
    end
    if(n%5==0) then
        out=out .. "Buzz"
    end
    if(out=="") then
        out=out .. tostring(n)
    end
    print(out)
    n=n+1
end
<?php
    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.strval($n);
        }
        echo ($out."\n");
    }
?>
for i := 1 to 100 do
begin
  Fizz := (i mod 3) = 0;
  Buzz := (i mod 5) = 0;
  if Fizz then write('Fizz');
  if Buzz then write('Buzz');
  if not (Fizz or Buzz) then write(i);
  write(' ');
end.
Delphi 7 - 11
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 my $i ( 1 .. 100 ) {
    print $i % 3 ? '' : 'Fizz';
    print $i % 5 ? '' : 'Buzz';
    print $i % 3 && $i % 5 ? $i : '';
    print "\n";
}
for i in range(100, 1):
    if i % 5 == 0 and not i % 3 == 0:
        print(i, "Buzz");
    if i % 3 == 0 and not i % 5 == 0:
        print(i, "Fizz");
    if i % 3 == 0 and i % 5 == 0:
        print(i, "FizzBuzz");
for i in range(1,101):
    if i % 15 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)
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
(1..100).each do |i|
  d3 = i % 3 == 0
  d5 = i % 5 == 0

  if d3 && d5
    puts "FizzBuzz"
  elsif d3
    puts "Fizz"
  elsif d5
    puts "Buzz"
  else
    puts "#{i}"
  end
end
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}"),
    }
}
1 to: 100 do: 
	[:ea | (ea isDivisibleBy: 15)
		ifTrue: [Transcript showln: 'FizzBuzz']
		ifFalse: [(ea isDivisibleBy: 5)
				ifTrue: [Transcript showln: 'Buzz']
				ifFalse: [(ea isDivisibleBy: 3)
						ifTrue: [Transcript showln: 'Fizz']
						ifFalse: [Transcript showln: ea]]]].

New implementation...
< >
fizzbuzz