Open In App

Fizz Buzz Implementation | Set 2

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:

Follow the below steps to solve the problem:

Below is the implementation of the above approach:




// C++ program for the above approach
#include <bits/stdc++.h>
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 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 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# 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




<script>
 
// JavaScript program for the above approach
 
// Function to generate FizzBuzz sequence
function fizzBuzz(N)
{
    // Stores count of multiples
    // of 3 and 5 respectively
    let count3 = 0;
    let count5 = 0;
  
    // Iterate from 1 to N
    for (let 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
        let flag = false;
  
        // Check if the value of count3
        // is equal to 3
        if (count3 == 3) {
            document.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) {
            document.write("Buzz");
  
            // Reset count5 to 0, and
            // set flag as True
            count5 = 0;
            flag = true;
        }
  
        // If none of the condition matches
        if (!flag) {
            document.write(i);
        }
  
        document.write(" ");
    }
}
 
// Driver Code
let N = 15;
fizzBuzz(N);
 
 
// This code is contributed by unknown2108
 
</script>

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

 

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


Article Tags :