Related Articles

# Fizz Buzz Implementation | Set 2

• Last Updated : 01 Jul, 2021

Given an integer N, the task is to print all the numbers from 1 to N replacing the multiples of 3, 5 and both 3 and 5 by “Fizz”, “Buzz” and “Fizz Buzz” respectively.

Examples:

Input: N = 5
Output: 1, 2, Fizz, 4, Buzz

Input: N = 15
Output: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14
Fizz Buzz

Approach using modulo operator: The simplest approach to solve this problem is to use modulo operator. Refer to the previous post of this article for this approach.

Approach without using modulo operator: The above problem can be solved without using the modulo operator because:

• Modulo operator is a very expensive operation. In the lowest level, modulo is a division.
• The DIV instruction of the compiler gives the division and modulo results. However, modern CPUs use specialized division circuits with lookup tables and so, there won’t be any significant change in speed by using bit-shifting.
• But for large integers, it is observed that the runtime is much slower in the modulo operation program than the other, with O(N2) computational complexity.

Follow the below steps to solve the problem:

• Initialize two count variables, say count3 and count5, to store the count of numbers divisible by 3 and 5 respectively.
• Iterate over the range [1, N] using a variable, say i, and perform the following steps:
• Increment count3 and count5 by 1.
• If the value of count3 is equal to 3, print “Fizz” and set count3 = 0.
• Similarly, if the value of count5 is equal to 5, print “Buzz” and set count5 = 0.
• If none of the above conditions match, then print i.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to generate FizzBuzz sequence``void` `fizzBuzz(``int` `N)``{``    ``// Stores count of multiples``    ``// of 3 and 5 respectively``    ``int` `count3 = 0;``    ``int` `count5 = 0;` `    ``// Iterate from 1 to N``    ``for` `(``int` `i = 1; i <= N; i++) {` `        ``// Increment count3 by 1``        ``count3++;` `        ``// Increment count5 by 1``        ``count5++;` `        ``// Initialize a boolean variable``        ``// to check if none of the``        ``// condition matches``        ``bool` `flag = ``false``;` `        ``// Check if the value of count3``        ``// is equal to 3``        ``if` `(count3 == 3) {``            ``cout << ``"Fizz"``;` `            ``// Reset count3 to 0, and``            ``// set flag as True``            ``count3 = 0;``            ``flag = ``true``;``        ``}` `        ``// Check if the value of count5``        ``// is equal  to 5``        ``if` `(count5 == 5) {``            ``cout << ``"Buzz"``;` `            ``// Reset count5 to 0, and``            ``// set flag as True``            ``count5 = 0;``            ``flag = ``true``;``        ``}` `        ``// If none of the condition matches``        ``if` `(!flag) {``            ``cout << i;``        ``}` `        ``cout << ``" "``;``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `N = 15;``    ``fizzBuzz(N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;``class` `GFG``{``  ` `// Function to generate FizzBuzz sequence``static` `void` `fizzBuzz(``int` `N)``{``  ` `    ``// Stores count of multiples``    ``// of 3 and 5 respectively``    ``int` `count3 = ``0``;``    ``int` `count5 = ``0``;` `    ``// Iterate from 1 to N``    ``for` `(``int` `i = ``1``; i <= N; i++) {` `        ``// Increment count3 by 1``        ``count3++;` `        ``// Increment count5 by 1``        ``count5++;` `        ``// Initialize a boolean variable``        ``// to check if none of the``        ``// condition matches``        ``boolean` `flag = ``false``;` `        ``// Check if the value of count3``        ``// is equal to 3``        ``if` `(count3 == ``3``) {``            ``System.out.print(``"Fizz"``);` `            ``// Reset count3 to 0, and``            ``// set flag as True``            ``count3 = ``0``;``            ``flag = ``true``;``        ``}` `        ``// Check if the value of count5``        ``// is equal  to 5``        ``if` `(count5 == ``5``) {``            ``System.out.print(``"Buzz"``);` `            ``// Reset count5 to 0, and``            ``// set flag as True``            ``count5 = ``0``;``            ``flag = ``true``;``        ``}` `        ``// If none of the condition matches``        ``if` `(!flag) {``            ``System.out.print(i);``        ``}` `        ``System.out.print(``" "``);``    ``}``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `N = ``15``;``    ``fizzBuzz(N);``}``}` `// This code is contributed by susmitakundugoaldanga.`

## Python3

 `# Python3 program for the above approach` `# Function to generate FizzBuzz sequence``def` `fizzBuzz(N):``    ` `    ``# Stores count of multiples``    ``# of 3 and 5 respectively``    ``count3 ``=` `0``    ``count5 ``=` `0` `    ``# Iterate from 1 to N``    ``for` `i ``in` `range``(``1``, N ``+` `1``):` `        ``# Increment count3 by 1``        ``count3 ``+``=` `1` `        ``# Increment count5 by 1``        ``count5 ``+``=` `1` `        ``# Initialize a boolean variable``        ``# to check if none of the``        ``# condition matches``        ``flag ``=` `False` `        ``# Check if the value of count3``        ``# is equal to 3``        ``if` `(count3 ``=``=` `3``):``            ``print` `(``"Fizz"``, end ``=` `"")` `            ``# Reset count3 to 0, and``            ``# set flag as True``            ``count3 ``=` `0``            ``flag ``=` `True` `        ``# Check if the value of count5``        ``# is equal  to 5``        ``if` `(count5 ``=``=` `5``):``            ``print` `(``"Buzz"``, end ``=` `"")` `            ``# Reset count5 to 0, and``            ``# set flag as True``            ``count5 ``=` `0``            ``flag ``=` `True` `        ``# If none of the condition matches``        ``if` `(``not` `flag):``            ``print` `(i, end ``=` `"")` `        ``print``(end ``=` `" "``)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``N ``=` `15``    ``fizzBuzz(N)` `    ``# This code is contributed by mohit kumar 29.`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{``  ` `// Function to generate FizzBuzz sequence``static` `void` `fizzBuzz(``int` `N)``{``    ` `    ``// Stores count of multiples``    ``// of 3 and 5 respectively``    ``int` `count3 = 0;``    ``int` `count5 = 0;` `    ``// Iterate from 1 to N``    ``for``(``int` `i = 1; i <= N; i++)``    ``{``        ` `        ``// Increment count3 by 1``        ``count3++;` `        ``// Increment count5 by 1``        ``count5++;` `        ``// Initialize a bool variable``        ``// to check if none of the``        ``// condition matches``        ``bool` `flag = ``false``;` `        ``// Check if the value of count3``        ``// is equal to 3``        ``if` `(count3 == 3)``        ``{``            ``Console.Write(``"Fizz"``);` `            ``// Reset count3 to 0, and``            ``// set flag as True``            ``count3 = 0;``            ``flag = ``true``;``        ``}` `        ``// Check if the value of count5``        ``// is equal  to 5``        ``if` `(count5 == 5)``        ``{``            ``Console.Write(``"Buzz"``);` `            ``// Reset count5 to 0, and``            ``// set flag as True``            ``count5 = 0;``            ``flag = ``true``;``        ``}` `        ``// If none of the condition matches``        ``if` `(!flag)``        ``{``            ``Console.Write(i);``        ``}``        ``Console.Write(``" "``);``    ``}``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``int` `N = 15;``    ` `    ``fizzBuzz(N);``}``}` `// This code is contributed by shikhasingrajput`

## Javascript

 ``
Output:
`1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz`

Time Complexity: O(N)
Auxiliary Space: O(1)

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.

My Personal Notes arrow_drop_up