Fizz Buzz Implementation

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/C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to print Fizz Buzz
#include <stdio.h>
  
int main(void)
{
    int i;
    for (i=1; i<=100; i++)
    {
        // number divisible by 3 and 5 will
        // always be divisible by 15, print 
        // 'FizzBuzz' in place of the number
        if (i%15 == 0)        
            printf ("FizzBuzz\t");    
          
        // number divisible by 3? print 'Fizz'
        // in place of the number
        else if ((i%3) == 0)    
            printf("Fizz\t");                 
          
        // number divisible by 5, print 'Buzz'  
        // in place of the number
        else if ((i%5) == 0)                       
            printf("Buzz\t");                 
      
        else // print the number            
            printf("%d\t", i);                 
  
    }
  
    return 0;
}

chevron_right


C++ Using STL

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to print Fizz Buzz
#include <iostream>
#include <algorithm>
#include <vector>
    
int main()
{
    // dynamic array(range) of size 100 of int type
    std :: vector<int> range(100);   
  
    // 'iota' to fill the vector in increasing manner
    std :: iota(range.begin(), range.end(), 1);  
  
    // initializing dynamic array of string type
    std :: vector<std::string> values;  
  
    // resize the vector(values) as that of vector(range) 
    values.resize(range.size());  
  
    //'auto' to deduce the type of the variable 
    auto fizzbuzz = [](int i) -> std::string                 
    {
        // number divisible by 15(will also be 
        // divisible by both 3 and 5), print 'FizzBuzz'
        if ((i%15) == 0)
            return "FizzBuzz";
   
        // number divisible by 5, print 'Buzz'
        if ((i%5) == 0)
            return "Buzz";
  
        // number divisible by 3, print 'Fizz'
        if ((i%3) == 0)
            return "Fizz";
  
        // to print other numbers
        return std::to_string(i);
    };
  
    // Operation to each of the elements in the 
    // range [begin(), end()) and stores the 
    // value returned by each operation in the 
    // range that begins at values.begin().
    std :: transform(range.begin(), range.end(), 
                    values.begin(), fizzbuzz);    
    for (auto& str: values)                                                   
        std::cout << str << std::endl;
  
    return 0;                                                                                    
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print Fizz Buzz
import java.util.*;
class FizzBuzz
{
    public static void main(String args[])
    
        int n = 100;
  
        // loop for 100 times
        for (int i=1; i<=n; i++)                                 
        {
            if (i%15==0)                                                 
                System.out.print("FizzBuzz"+" "); 
            // number divisible by 5, print 'Buzz' 
            // in place of the number
            else if (i%5==0)     
                System.out.print("Buzz"+" "); 
  
            // number divisible by 3, print 'Fizz' 
            // in place of the number
            else if (i%3==0)     
                System.out.print("Fizz"+" "); 
  
            // number divisible by 15(divisible by
            // both 3 & 5), print 'FizzBuzz' in 
            // place of the number
                  
            else // print the numbers
                System.out.print(i+" ");                         
        }
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print Fizz Buzz
using System;
  
class GFG
{
    // Driver Code
    public static void Main()
    
        int n = 100;
  
        // loop for 100 times
        for (int i = 1; i <= n; i++)                             
        {
            // number divisible by 5, 
            // print 'Buzz' in place
            // of the number
            if (i % 15 == 0) 
                Console.Write("FizzBuzz" + " "); 
  
            // number divisible by 3, 
            // print 'Fizz' in place 
            // of the number
            else if (i % 3 == 0)     
                Console.Write("Fizz" + " "); 
  
            // number divisible by 
            // 15(divisible by both 
            // 3 & 5), print 'FizzBuzz'
            // in place of the number
            else if (i % 5 == 0)                                             
                Console.Write("Buzz" + " "); 
              
            // print the numbers 
            else
                Console.Write(i + " ");                     
        }
    }
}
  
// This code is contributed
// by shiv_bhakt.

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

#Python program to print Fizz Buzz
#loop for 100 times i.e. range
for fizzbuzz in range(100): 
  
    # number divisible by 3, print 'Fizz' 
    # in place of the number
    if fizzbuzz % 15 == 0
        print("FizzBuzz")                                         
        continue
  
    # number divisible by 5, print 'Buzz'
    # in place of the number
    elif fizzbuzz % 3 == 0:     
        print("Fizz")                                         
        continue
  
    # number divisible by 15 (divisible 
    # by both 3 & 5), print 'FizzBuzz' in
    # place of the number
    elif fizzbuzz % 5 == 0:         
        print("Buzz")                                     
        continue
  
    # print numbers
    print(fizzbuzz)

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to print Fizz Buzz
  
$i;
for ($i = 1; $i <= 100; $i++)
{
    // number divisible by 3 and
    // 5 will always be divisible 
    // by 15, print 'FizzBuzz' in
    // place of the number
    if ($i % 15 == 0) 
        echo "FizzBuzz" . "  "
      
    // number divisible by 3? print 
    // 'Fizz' in place of the number
    else if (($i % 3) == 0) 
        echo "Fizz" . "  ";             
      
    // number divisible by 5, print 
    // 'Buzz' in place of the number
    else if (($i % 5) == 0)                 
        echo "Buzz" . "  ";             
  
    else // print the number         
        echo $i,"  " ;             
}
  
// This code is contributed by m_kit
?>

chevron_right



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

  1. 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, …

  2. 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, …

  3. 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, …

  4. 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.
  5. 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), …

  6. Reference :
    https://en.wikipedia.org/wiki/Fizz_buzz

    This article is contributed by Kishlay Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



    My Personal Notes arrow_drop_up

    Improved By : jit_t, Vishal_Khoda, prash_pach



    Article Tags :
    Practice Tags :


    5


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.