Skip to content
Related Articles

Related Articles

Fizz Buzz Implementation

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 28 Jul, 2022

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




// 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;
}

C++ Using STL




// 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;                                                                                   
}

Java




// 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++)                                
        {
            //number divisible by 15(divisible by
            // both 3 & 5), print 'FizzBuzz' in
            // place of the number
            if (i%15==0)                                                
                System.out.println("FizzBuzz");
            // number divisible by 5, print 'Buzz'
            // in place of the number
            else if (i%5==0)    
                System.out.println("Buzz");
 
            // number divisible by 3, print 'Fizz'
            // in place of the number
            else if (i%3==0)    
                System.out.println("Fizz");
                 
            else // print the numbers
                System.out.println(i);                        
        }
    }
}

Python3




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

C#




// 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 15(
        // divisible by both 3 & 5),
        // print 'FizzBuzz' 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
        // 5, print 'Buzz'
        // 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.

PHP




<?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
?>

Javascript




<script>
 
// JavaScript program to print Fizz Buzz
 
    let 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)   
            document.write("FizzBuzz" + " ");
         
        // number divisible by 3? print 'Fizz'
        // in place of the number
        else if ((i%3) == 0)
            document.write("Fizz" + " ");               
         
        // number divisible by 5, print 'Buzz'
        // in place of the number
        else if ((i%5) == 0)                   
            document.write("Buzz" + " ");               
     
        else // print the number       
            document.write(i + " ");               
    }
 
//This code is contributed by Manoj
</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    

Time Complexity: O(100)

Auxiliary Space: O(1)

Optimization of Fizz Buzz problem

The modulo operator is a very costly operation compared to other arithmetic operations and i%15 is intrepreted somehow like i%3 and i%5 hence it is a costly way to solve the problem in terms of time complexity. We can solve the same using simple addition operations by sacrificing an extra variable space. Below is the optimized code for the same.

C++




// Optimized C++ program for fizz buzz problem
#include <iostream>
#include <string>
using namespace std;
 
int main()
{
    string s = "";
    int c3 = 0, c5 = 0;
    for (int i = 1; i <= 100; i++) {
        c3++;
        c5++;
        if (c3 == 3) {
            s += "fizz";
            c3 = 0;
        }
        if (c5 == 5) {
            s += "buzz";
            c5 = 0;
        }
        if (s.empty())
            cout << i << endl;
        else
            cout << s << endl;
        s = "";
    }
 
    return 0;
}

Java




// Optimized Java program for fizz buzz problem
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        String s = "";
        int c3 = 0, c5 = 0;
        for (int i = 1; i <= 100; i++) {
            c3++;
            c5++;
            if (c3 == 3) {
                s += "fizz";
                c3 = 0;
            }
            if (c5 == 5) {
                s += "buzz";
                c5 = 0;
            }
            if (s.length() == 0)
                System.out.println(i);
            else
                System.out.println(s);
            s = "";
        }
    }
}
// This code is contributed by Karandeep1234

Python3




# code
c3 = 0
c5 = 0
d = ""
for i in range(1, 101):
    c3 += 1
    c5 += 1
    if(c3 == 3):
        d += "fizz"
        c3 = 0
    if(c5 == 5):
        d += "buzz"
        c5 = 0
    if(d == ""):
        print(i)
    else:
        print(d)
    d = ""

C#




// Optimized C# program for fizz buzz problem
using System;
 
class GFG {
    public static void Main(string[] args)
    {
        string s = "";
        int c3 = 0, c5 = 0;
        for (int i = 1; i <= 100; i++) {
            c3++;
            c5++;
            if (c3 == 3) {
                s += "fizz";
                c3 = 0;
            }
            if (c5 == 5) {
                s += "buzz";
                c5 = 0;
            }
            if (s.Length == 0)
                Console.WriteLine(i);
            else
                Console.WriteLine(s);
            s = "";
        }
    }
}
 
// This code is contributed by phasing17

Javascript




// Optimized JavaScript program for fizz buzz problem
let s = "";
let c3 = 0, c5 = 0;
for (var i = 1; i <= 100; i++) {
    c3++;
    c5++;
    if (c3 == 3) {
        s += "fizz";
        c3 = 0;
    }
    if (c5 == 5) {
        s += "buzz";
        c5 = 0;
    }
    if (s.length == 0)
        console.log(i);
    else
        console.log(s);
    s = "";
}
 
// This code is contributed by phasing17

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

Time Complexity: O(100)

Auxiliary Space: O(1)

Variations of Fizz Buzz to Try

  1. Replace numbers 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 examples 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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!