Fizz Buzz is a very simple programming task, asked in software developer job interviews.
A typical round of Fizz Buzz can be:
Write a program that prints the numbers from 1 to 100 and for multiples of ‘3’ print “Fizz” instead of the number and for the multiples of ‘5’ print “Buzz”.
1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14,
Fizz Buzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26,
Fizz, 28, 29, Fizz Buzz, 31, 32, Fizz, 34, Buzz, Fizz, ...
C++
#include <stdio.h>
int main(void)
{
int i;
for (i=1; i<=100; i++)
{
if (i%15 == 0)
printf ("FizzBuzz\t");
else if ((i%3) == 0)
printf("Fizz\t");
else if ((i%5) == 0)
printf("Buzz\t");
else
printf("%d\t", i);
}
return 0;
}
|
C++ Using STL
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
std :: vector<int> range(100);
std :: iota(range.begin(), range.end(), 1);
std :: vector<std::string> values;
values.resize(range.size());
auto fizzbuzz = [](int i) -> std::string
{
if ((i%15) == 0)
return "FizzBuzz";
if ((i%5) == 0)
return "Buzz";
if ((i%3) == 0)
return "Fizz";
return std::to_string(i);
};
std :: transform(range.begin(), range.end(),
values.begin(), fizzbuzz);
for (auto& str: values)
std::cout << str << std::endl;
return 0;
}
|
Java
import java.util.*;
class FizzBuzz
{
public static void main(String args[])
{
int n = 100;
for (int i=1; i<=n; i++)
{
if (i%15==0)
System.out.print("FizzBuzz"+" ");
else if (i%5==0)
System.out.print("Buzz"+" ");
else if (i%3==0)
System.out.print("Fizz"+" ");
else
System.out.print(i+" ");
}
}
}
|
Python
for fizzbuzz in range(1,100):
if fizzbuzz % 15 == 0:
print("FizzBuzz")
continue
elif fizzbuzz % 3 == 0:
print("Fizz")
continue
elif fizzbuzz % 5 == 0:
print("Buzz")
continue
print(fizzbuzz)
|
C#
using System;
class GFG{
public static void Main()
{
int n = 100;
for(int i = 1; i <= n; i++)
{
if (i % 15 == 0)
Console.Write("FizzBuzz" + " ");
else if (i % 3 == 0)
Console.Write("Fizz" + " ");
else if (i % 5 == 0)
Console.Write("Buzz" + " ");
else
Console.Write(i + " ");
}
}
}
|
PHP
<?php
$i;
for ($i = 1; $i <= 100; $i++)
{
if ($i % 15 == 0)
echo "FizzBuzz" . " ";
else if (($i % 3) == 0)
echo "Fizz" . " ";
else if (($i % 5) == 0)
echo "Buzz" . " ";
else
echo $i," " ;
}
?>
|
Javascript
<script>
let i;
for (i=1; i<=100; i++)
{
if (i%15 == 0)
document.write("FizzBuzz" + " ");
else if ((i%3) == 0)
document.write("Fizz" + " ");
else if ((i%5) == 0)
document.write("Buzz" + " ");
else
document.write(i + " ");
}
</script>
|
Output :
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11
Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz
Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz
31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41
Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz
52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61
62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71
Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz
82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91
92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz
Variations of Fizz Buzz to Try
- Replace number containing digit 3 or 5: Instead of replacing numbers that have 3 or 5 as a factor, the game can be played by replacing numbers containing the digit 3 or 5 with “fizz” or “buzz”.
For Example: 1, 2, Fizz, 4, Buzz, 6, 7, 8, 9, 10, 11, 12, Fizz, 14, Buzz, 16, 17, 18, 19, 20, 21, 22, Fizz, 24, Buzz, 26, 27, 28, 29, Fizz, Fizz, Fizz, Fizz, Fizz, Fizz Buzz, Fizz, Fizz, Fizz, Fizz, 40, 41, 42, Fizz, 44, Buzz, … - Replace the digits(3 or 5) only in the number: In this variation, only the actual digit is replaced, so 23 becomes “twenty-fizz” and 50 is “buzzty”. This variation can be combined with the original to form an even more challenging sequence.
For Example: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, Fizz, 14, Fizz Buzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, Fizz, Fizz, Buzz, 26, Fizz, 28, 29, Fizz Buzz, Fizz, Fizz, Fizz, Fizz, Fizz, Fizz Buzz, Fizz, Fizz, Fizz, Fizz, Buzz, 41, Fizz, Fizz, 44, Fizz Buzz, … - Change of Base:The game can be played in a mathematical base other than 10. For example, playing in base 5 would proceed as follows:
For Example: 1, 2, Fizz, 4, Buzz, Fizz, 12, 13, Fizz, Buzz, 21, Fizz, 23, 24, Fizz Buzz, 31, 32, Fizz, 34, Buzz, Fizz, … - Fizz or Buzz, but not Fizz-Buzz:More challenging variation has the direction of play change on Fizz or Buzz, but not on Fizz-Buzz. For certain sequences, this makes the action bounce between 2 or 3 players and causes misplays when it breaks out. The 3/7 version of the game has a complex sequence between 12 and 18, for example.
- Fizz Buzz Woof:One variation has expanded to such an extent as to have its own related name. In this case, the number 3 becomes Fizz, 5 becomes Buzz, and 7 becomes Woof. The main rules in this game are that any number that contains the number or is divisible by that number is replaced by an occurrence of the word. If the number has 2 instances of that number (i.e. 33 or 55) and is divisible by that number, then the word is said three times in this example. The additional rule is that the words (if more than one occur) must be said in the order given in the title.
For example: 1, 2, Fizz Fizz (3), 4, Buzz Buzz (5), Fizz (6), Woof Woof (7), 8, Fizz (9), Buzz (10), 11, Fizz (12), Fizz (13), Woof (14), Fizz Buzz Buzz (15), 16, Woof (17), Fizz (18), 19, Buzz (20), Fizz Woof (21), 22, Fizz (23), Fizz (24), Buzz Buzz (25), 26, Fizz Woof (27), Woof (28), 29, Fizz Fizz Buzz (30), Fizz (31), Fizz (32), Fizz Fizz Fizz (33), …
Reference :
https://en.wikipedia.org/wiki/Fizz_buzz
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.