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++
#include <bits/stdc++.h>
using namespace std;
void fizzBuzz( int N)
{
int count3 = 0;
int count5 = 0;
for ( int i = 1; i <= N; i++) {
count3++;
count5++;
bool flag = false ;
if (count3 == 3) {
cout << "Fizz" ;
count3 = 0;
flag = true ;
}
if (count5 == 5) {
cout << "Buzz" ;
count5 = 0;
flag = true ;
}
if (!flag) {
cout << i;
}
cout << " " ;
}
}
int main()
{
int N = 15;
fizzBuzz(N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void fizzBuzz( int N)
{
int count3 = 0 ;
int count5 = 0 ;
for ( int i = 1 ; i <= N; i++) {
count3++;
count5++;
boolean flag = false ;
if (count3 == 3 ) {
System.out.print( "Fizz" );
count3 = 0 ;
flag = true ;
}
if (count5 == 5 ) {
System.out.print( "Buzz" );
count5 = 0 ;
flag = true ;
}
if (!flag) {
System.out.print(i);
}
System.out.print( " " );
}
}
public static void main(String[] args)
{
int N = 15 ;
fizzBuzz(N);
}
}
|
Python3
def fizzBuzz(N):
count3 = 0
count5 = 0
for i in range ( 1 , N + 1 ):
count3 + = 1
count5 + = 1
flag = False
if (count3 = = 3 ):
print ( "Fizz" , end = "")
count3 = 0
flag = True
if (count5 = = 5 ):
print ( "Buzz" , end = "")
count5 = 0
flag = True
if ( not flag):
print (i, end = "")
print (end = " " )
if __name__ = = '__main__' :
N = 15
fizzBuzz(N)
|
C#
using System;
class GFG{
static void fizzBuzz( int N)
{
int count3 = 0;
int count5 = 0;
for ( int i = 1; i <= N; i++)
{
count3++;
count5++;
bool flag = false ;
if (count3 == 3)
{
Console.Write( "Fizz" );
count3 = 0;
flag = true ;
}
if (count5 == 5)
{
Console.Write( "Buzz" );
count5 = 0;
flag = true ;
}
if (!flag)
{
Console.Write(i);
}
Console.Write( " " );
}
}
public static void Main(String[] args)
{
int N = 15;
fizzBuzz(N);
}
}
|
Javascript
<script>
function fizzBuzz(N)
{
let count3 = 0;
let count5 = 0;
for (let i = 1; i <= N; i++) {
count3++;
count5++;
let flag = false ;
if (count3 == 3) {
document.write( "Fizz" );
count3 = 0;
flag = true ;
}
if (count5 == 5) {
document.write( "Buzz" );
count5 = 0;
flag = true ;
}
if (!flag) {
document.write(i);
}
document.write( " " );
}
}
let N = 15;
fizzBuzz(N);
</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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...