Fizz Buzz Program in C++, Java, Python, C# & Javascript
Last Updated :
10 Jan, 2024
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
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)
{
vector<string> result;
for ( int i = 1; i <= n; ++i) {
if (i % 3 == 0 && i % 5 == 0) {
result.push_back( "FizzBuzz" );
}
else if (i % 3 == 0) {
result.push_back( "Fizz" );
}
else if (i % 5 == 0) {
result.push_back( "Buzz" );
}
else {
result.push_back(to_string(i));
}
}
return result;
}
int main()
{
int n = 20;
vector<string> result = fizzBuzz(n);
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)
{
List<String> result = new ArrayList<>();
for ( int i = 1 ; i <= n; ++i) {
if (i % 3 == 0 && i % 5 == 0 ) {
result.add( "FizzBuzz" );
}
else if (i % 3 == 0 ) {
result.add( "Fizz" );
}
else if (i % 5 == 0 ) {
result.add( "Buzz" );
}
else {
result.add(Integer.toString(i));
}
}
return result;
}
public static void main(String[] args)
{
int n = 20 ;
List<String> result = fizzBuzz(n);
for (String s : result) {
System.out.print(s + " " );
}
}
}
|
Python
def fizz_buzz(n):
result = []
for i in range ( 1 , n + 1 ):
if i % 3 = = 0 and i % 5 = = 0 :
result.append( "FizzBuzz" )
elif i % 3 = = 0 :
result.append( "Fizz" )
elif i % 5 = = 0 :
result.append( "Buzz" )
else :
result.append( str (i))
return result
n = 20
result = fizz_buzz(n)
print ( ' ' .join(result))
|
C#
using System;
using System.Collections.Generic;
class Program {
static List< string > FizzBuzz( int n)
{
List< string > result = new List< string >();
for ( int i = 1; i <= n; ++i) {
if (i % 3 == 0 && i % 5 == 0) {
result.Add( "FizzBuzz" );
}
else if (i % 3 == 0) {
result.Add( "Fizz" );
}
else if (i % 5 == 0) {
result.Add( "Buzz" );
}
else {
result.Add(i.ToString());
}
}
return result;
}
static void Main( string [] args)
{
int n = 20;
List< string > result = FizzBuzz(n);
foreach ( string s in result)
{
Console.Write(s + " " );
}
}
}
|
Javascript
function fizzBuzz(n) {
let result = [];
for (let i = 1; i <= n; ++i) {
if (i % 3 === 0 && i % 5 === 0) {
result.push( "FizzBuzz" );
}
else if (i % 3 === 0) {
result.push( "Fizz" );
}
else if (i % 5 === 0) {
result.push( "Buzz" );
}
else {
result.push(i.toString());
}
}
return result;
}
let n = 20;
let result = fizzBuzz(n);
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)
Share your thoughts in the comments
Please Login to comment...