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++
// 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.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" + " " ); else // print the numbers System.out.print(i+ " " ); } } } |
Python
# Python program to print Fizz Buzz # Loop for 100 times i.e. range for fizzbuzz in range ( 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 ?> |
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 DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.