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.
Example:
Input: n = 3
Output: [1 2 Fizz]
Input: n = 5
Output: [1 2 Fizz 4 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:
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
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
31 Oct, 2023
Like Article
Save Article