Logo

Programming-Idioms

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"

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);
}
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));
}
  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
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 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.
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(1,101):
    if i % 15 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)
(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