10+ Programming Languages FizzBuzz Program Examples

10+ Programming Languages FizzBuzz Program Examples

Nowadays, most interviewers ask the candidate to write the FizzBuzz program. So here I am, giving the 10+ programming languages FizzBuzz program examples. These examples will help you learn how to do it in the most popular programming languages.

What is FizzBuzz Program?

In the FizzBuzz program, you have to write the following logic:

  1. For multiples of three, print Fizz (instead of the number).
  2. For multiples of five, print Buzz (instead of the number).
  3. For multiples of both three and five, print FizzBuzz (instead of the number).

FizzBuzz Program Examples

Below are the 10+ FizzBuzz program examples from the most common programming languages.

Example 1: Using C++

#include <iostream>
using namespace std;
 
int main()
{
  for (int i = 0; i <= 100; ++i)
  {
    bool fizz = (i % 3) == 0;
    bool buzz = (i % 5) == 0;
    if (fizz)
      cout << "Fizz";
    if (buzz)
      cout << "Buzz";
    if (!fizz && !buzz)
      cout << i;
    cout << "\n";
  }
  return 0;
}

Output:

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

Code Explanation

  •  The code is a loop that iterates from 1 to 100.
  •  The code checks if the number is divisible by 3, 5, or 15 and prints out "FizzBuzz" for numbers divisible by 3, "Fizz" for numbers divisible by 5, and "Buzz" for numbers not divisible by either of those values.
  •  The code will print the numbers from 1 to 100.
  •  If a number is divisible by 3, it will be printed as "Fizz".
  •  If a number is divisible by 5, it will be printed as "Buzz".
  •  Otherwise, the number itself will be printed.

Example 2: Using GO Lang

package main
 
import "fmt"
 
func main() {
    for i := 1; i <= 100; i++ {
        switch {
        case i%15==0:
            fmt.Println("FizzBuzz")
        case i%3==0:
            fmt.Println("Fizz")
        case i%5==0:
            fmt.Println("Buzz")
        default: 
            fmt.Println(i)
        }
    }
}

The result would be the same as is in the first example.

Example 3: Using Java

public class FizzBuzz {
    public static void main(String[] args) {
        for (int number = 1; number <= 100; number++) {
            if (number % 15 == 0) {
                System.out.println("FizzBuzz");
            } else if (number % 3 == 0) {
                System.out.println("Fizz");
            } else if (number % 5 == 0) {
                System.out.println("Buzz");
            } else {
                System.out.println(number);
            }
        }
    }
}

Example 4: Using JavaScript

var fizzBuzz = function () {
  var i, output;
  for (i = 1; i < 101; i += 1) {
    output = '';
    if (!(i % 3)) { output += 'Fizz'; }
    if (!(i % 5)) { output += 'Buzz'; }
    console.log(output || i);//empty string is false, so we short-circuit
  }
};

Example 5: Using Julia

for i in 1:100
    if i % 15 == 0
        println("FizzBuzz")
    elseif i % 3 == 0
        println("Fizz")
    elseif i % 5 == 0
        println("Buzz")
    else
        println(i)
    end
end

Example 6: Using Kotlin

fun fizzBuzz() {
    for (number in 1..100) {
        println(
            when {
                number % 15 == 0 -> "FizzBuzz"
                number % 3 == 0 -> "Fizz"
                number % 5 == 0 -> "Buzz"
                else -> number
            }
        )
    }
}

Example 7: Using LaTex

\documentclass{minimal}
\usepackage{ifthen}
\usepackage{intcalc}
\newcounter{mycount}
\newboolean{fizzOrBuzz}
\newcommand\fizzBuzz[1]{%
\setcounter{mycount}{1}\whiledo{\value{mycount}<#1}
    {
    \setboolean{fizzOrBuzz}{false}
    \ifthenelse{\equal{\intcalcMod{\themycount}{3}}{0}}{\setboolean{fizzOrBuzz}{true}Fizz}{}
    \ifthenelse{\equal{\intcalcMod{\themycount}{5}}{0}}{\setboolean{fizzOrBuzz}{true}Buzz}{}
    \ifthenelse{\boolean{fizzOrBuzz}}{}{\themycount}
    \stepcounter{mycount}
    \\
    }
}
\begin{document}
\fizzBuzz{101}
\end{document}

Example 8: Using PHP

<?php
for ($i = 1; $i <= 100; $i++)
{
    if (!($i % 15))
        echo "FizzBuzz\n";
    else if (!($i % 3))
        echo "Fizz\n";
    else if (!($i % 5))
        echo "Buzz\n";
    else
        echo "$i\n";
}
?>

Example 9: Using PL/SQL

BEGIN
  FOR i IN 1 .. 100
  LOOP
    CASE
    WHEN MOD(i, 15) = 0 THEN
      DBMS_OUTPUT.put_line('FizzBuzz');
    WHEN MOD(i, 5) = 0 THEN
      DBMS_OUTPUT.put_line('Buzz');
    WHEN MOD(i, 3) = 0 THEN
      DBMS_OUTPUT.put_line('Fizz');
    ELSE
      DBMS_OUTPUT.put_line(i);
    END CASE;
  END LOOP;
END;

Example 10: Using Python

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)

Example 11: Using Oracle Select Statement (SQL)

SELECT CASE
    WHEN MOD(level,15)=0 THEN 'FizzBuzz'
    WHEN MOD(level,3)=0 THEN 'Fizz'
    WHEN MOD(level,5)=0 THEN 'Buzz'
    ELSE TO_CHAR(level)
    END FizzBuzz
    FROM dual
    CONNECT BY LEVEL <= 100;

See also: