Fizz Buzz Implementation in C++



In this problem, we will see the implementation and types of Fizz-Bizz problem.

Fizz Buzz − it is a simple programming problem in which the programmer changes the occurrence o all multiples of 3 by ‘Fizz’ and all multiples of 5 by ‘Buzz’ in the numbers from 1 to 100.

Let’s take an example to understand the problem

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

Solution Approach

A simple approach to solving the problem is by simply using a loop from 1 to 100. And then in each iteration check for both the below conditions separately,

Condition 1 − if i is divisible by 3, replace the count with ‘Fizz’.

Condition 2 − if i is divisible by 5, replace the count with ‘Buzz’.

Otherwise, print the number. For the values where the number is divisible by both 3 and 5. We will print fizz buzz.

Example

Program to illustrate the working of our solution

#include <iostream>
using namespace std;

int main(){

   for (int i=1; i<=100; i++){

      if (i%15 == 0)
         cout<<"Fizz Buzz,\t";
      else if ((i%3) == 0)
         cout<<"Fizz,\t";
      else if ((i%5) == 0)
         cout<<"Buzz,\t";
      else
         cout<<i<<",\t";
   }
   return 0;
}

Output

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, 37, 38, Fizz, Buzz, 41, Fizz, 43,
44, Fizz Buzz, 46, 47, Fizz, 49, Buzz, Fizz, 52, 53, Fizz, Buzz, 56, Fizz, 58, 59, Fizz Buzz, 61, 62, Fizz,
64, Buzz, Fizz, 67, 68, Fizz, Buzz, 71, Fizz, 73, 74, Fizz Buzz, 76, 77, Fizz, 79, Buzz, Fizz, 82, 83, Fizz,
Buzz, 86, Fizz, 88, 89, Fizz Buzz, 91, 92, Fizz, 94, Buzz, Fizz, 97, 98, Fizz, Buzz,

Some Other Variations of the Fizz Buzz problem

The Fizz Buzz problem is a common programming problem commonly used in programming interviews to check the programmer's logic. Over time the problem has got some upgrades to make the problem better and sometimes more difficult to solve. Here, are some of the common variations of Fizz Buzz problem. 

  • Digit Based Fizz Buzz − In this problem, the programmer needs to change the value to fizz buzz based on the occurrence of 3 or 5 being a digit of the number instead of checking for factors.

    Example − 1, 2, Fizz(3), 4, Buzz(5), 6, 7, 8, 9, 10, 11, 12, Fizz(13), 14, Buzz(15), 16, 17, 18, 19, 20, 21, 22, Fizz(23), 24, Buzz(25), 26, 27, 28, 29, Fizz(30), Fizz(31), Fizz(32), Fizz(33), Fizz(34), Fizz Buzz(35),...

  • Fizz Buzz Woof − This variation of the problem adds one more word which is woof for the next prime number 7. Now, we will be changing values for factors 3, 5, and 7.

    Example − 1, 2, Fizz (3), 4, Buzz (5), Fizz (6), Woof (7), 8, Fizz (9), Buzz (10), 11, Fizz (12), 13, Woof (14), Buzz (15), 16, 17, Fizz (18), 19, Buzz (20), Fizz Woof (21), 22, 23, Fizz (24), Buzz (25), 26, Fizz Woof (27), Woof (28), 29, Buzz (30), 31, 32, Fizz (33), 34, Fizz Buzz woof (35)...

  • Fizz Buzz for other base numbers − One more variation of the game is by simply changing the based on the number of another base in order to make the problem a bit more complex.

    Example − changing to base 16 (hexadecimal) 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, B, Fizz, D, E, Fizz Buzz, 11, Fizz,....

  • Combining two or more variations − The problem can be made more complex by combining two or more variations of the problem. Like a Fizz Buzz considering both factors as well as digits for changing to fizz buzz.


Advertisements