Open In App

Fizz Buzz Program in C++, Java, Python, C# & Javascript

Improve
Improve
Like Article
Like
Save
Share
Report

Fizz Buzz Problem states that given an integer n, for every integer i <= n, the task is to print “FizzBuzz” if i is divisible by 3 and 5, “Fizz” if i is divisible by 3, “Buzz” if i is divisible by 5 or i (as a string) if none of the conditions are true.

Fizz Buzz Program

Fizz Buzz Program

Example:

Input: n = 3
Output: [1 2 Fizz]

Input: n = 10
Output: [1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz]

Input: n = 20
Output: [1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz]

Approach for Fizz Buzz Program:

Iterate on the given number from 1 to n, check its divisibility and add the string into result according to the given condition.

  • Initialize an empty result list.
  • Iterate on the given number from 1 to n
    • For every number, if it is divisible by both 3 and 5, add FizzBuzz to the result list.
    • Else, Check if the number is divisible by 3, add Fizz.
    • Else, Check if the number is divisible by 5, add Buzz.
    • Else, add the number by converting it to string.
  • Print the result.

Below is the implementation of the above approach:

C++




#include <bits/stdc++.h>
using namespace std;
  
vector<string> fizzBuzz(int n)
{
    // Declare a vector of strings to store the results
    vector<string> result;
  
    // Loop from 1 to n (inclusive)
    for (int i = 1; i <= n; ++i) {
  
        // Check if i is divisible by both 3 and 5
        if (i % 3 == 0 && i % 5 == 0) {
  
            // Add "FizzBuzz" to the result vector
            result.push_back("FizzBuzz");
        }
  
        // Check if i is divisible by 3
        else if (i % 3 == 0) {
  
            // Add "Fizz" to the result vector
            result.push_back("Fizz");
        }
  
        // Check if i is divisible by 5
        else if (i % 5 == 0) {
  
            // Add "Buzz" to the result vector
            result.push_back("Buzz");
        }
        else {
  
            // Add the current number as a string to the
            // result vector
            result.push_back(to_string(i));
        }
    }
  
    // Return the result vector
    return result;
}
  
// Driver code
int main()
{
    int n = 20;
  
    // Call the fizzBuzz function to get the result
    vector<string> result = fizzBuzz(n);
  
    // Print the result
    for (const string& s : result) {
        cout << s << " ";
    }
  
    return 0;
}


Java




import java.util.ArrayList;
import java.util.List;
  
public class FizzBuzz {
    public static List<String> fizzBuzz(int n)
    {
        // Declare a list of strings to store the results
        List<String> result = new ArrayList<>();
  
        // Loop from 1 to n (inclusive)
        for (int i = 1; i <= n; ++i) {
            
            // Check if i is divisible by both 3 and 5
            if (i % 3 == 0 && i % 5 == 0) {
                
                // Add "FizzBuzz" to the result list
                result.add("FizzBuzz");
            }
            
            // Check if i is divisible by 3
            else if (i % 3 == 0) {
                
                // Add "Fizz" to the result list
                result.add("Fizz");
            }
            
            // Check if i is divisible by 5
            else if (i % 5 == 0) {
                
                // Add "Buzz" to the result list
                result.add("Buzz");
            }
            else {
                
                // Add the current number as a string to the
                // result list
                result.add(Integer.toString(i));
            }
        }
  
        // Return the result list
        return result;
    }
  
    public static void main(String[] args)
    {
        int n = 20;
  
        // Call the fizzBuzz function to get the result
        List<String> result = fizzBuzz(n);
  
        // Print the result
        for (String s : result) {
            System.out.print(s + " ");
        }
    }
}


Python




def fizz_buzz(n):
    # Declare a list of strings to store the results
    result = []
  
    # Loop from 1 to n (inclusive)
    for i in range(1, n + 1):
  
        # Check if i is divisible by both 3 and 5
        if i % 3 == 0 and i % 5 == 0:
  
            # Add "FizzBuzz" to the result list
            result.append("FizzBuzz")
  
        # Check if i is divisible by 3
        elif i % 3 == 0:
  
            # Add "Fizz" to the result list
            result.append("Fizz")
  
        # Check if i is divisible by 5
        elif i % 5 == 0:
  
            # Add "Buzz" to the result list
            result.append("Buzz")
        else:
  
            # Add the current number as a string to the
            # result list
            result.append(str(i))
  
    # Return the result list
    return result
  
  
# Driver code
n = 20
  
# Call the fizz_buzz function to get the result
result = fizz_buzz(n)
  
# Print the result
print(' '.join(result))


C#




using System;
using System.Collections.Generic;
  
class Program {
    static List<string> FizzBuzz(int n)
    {
        // Declare a list of strings to store the results
        List<string> result = new List<string>();
  
        // Loop from 1 to n (inclusive)
        for (int i = 1; i <= n; ++i) {
            // Check if i is divisible by both 3 and 5
            if (i % 3 == 0 && i % 5 == 0) {
                // Add "FizzBuzz" to the result list
                result.Add("FizzBuzz");
            }
            // Check if i is divisible by 3
            else if (i % 3 == 0) {
                // Add "Fizz" to the result list
                result.Add("Fizz");
            }
            // Check if i is divisible by 5
            else if (i % 5 == 0) {
                // Add "Buzz" to the result list
                result.Add("Buzz");
            }
            else {
                // Add the current number as a string to the
                // result list
                result.Add(i.ToString());
            }
        }
  
        // Return the result list
        return result;
    }
  
    static void Main(string[] args)
    {
        int n = 20;
  
        // Call the FizzBuzz function to get the result
        List<string> result = FizzBuzz(n);
  
        // Print the result
        foreach(string s in result)
        {
            Console.Write(s + " ");
        }
    }
}


Javascript




function fizzBuzz(n) {
    // Declare an array to store the results
    let result = [];
  
    // Loop from 1 to n (inclusive)
    for (let i = 1; i <= n; ++i) {
      
        // Check if i is divisible by both 3 and 5
        if (i % 3 === 0 && i % 5 === 0) {
          
            // Add "FizzBuzz" to the result array
            result.push("FizzBuzz");
        
          
        // Check if i is divisible by 3
        else if (i % 3 === 0) {
          
            // Add "Fizz" to the result array
            result.push("Fizz");
        
          
        // Check if i is divisible by 5
        else if (i % 5 === 0) {
          
            // Add "Buzz" to the result array
            result.push("Buzz");
        
        else {
          
            // Add the current number as a string to the
            // result array
            result.push(i.toString());
        }
    }
  
    // Return the result array
    return result;
}
  
// Driver code
let n = 20;
  
// Call the fizzBuzz function to get the result
let result = fizzBuzz(n);
  
// Print the result
console.log(result.join(' '));


Output

1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz 

Complexity Analysis of Fizz Buzz Program:

Time Complexity: O(N), since we need to traverse the numbers from 1 to n in any condition.
Auxiliary Space: O(1)



Last Updated : 10 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads