Table of Contents
Introduction
FizzBuzz is a popular game played among small kids, which helps them learn division. Over time this game has become a popular interview question for Software Development Engineer job interviews. It is one of the most basic problems in the coding interview world.
The FizzBuzz program acts as the litmus test to select suitable candidates in the initial rounds. This program is enough to judge the for loop and conditional branching concepts of any programmer.
What is FizzBuzz?
Given an integer n, print the appropriate result for the various numbers from 1 to n. The rules to write a FizzBuzz program are:
- Print Fizz if the number is divisible by 3
- Print Buzz if the number is divisible by 5
- Print FizzBuzz if the number is divisible by both 3 and 5.
- Print the number if the number is not divisible by 3 and 5.
FizzBuzz Program Implementation
1. Naive Approach: Using the modulo operator
The FizzBuzz program can be written using the modulo operator and if-else statements. In this approach, the divisibility test for 15, 3, and 5 are done individually to print the required output.
Pseudo Code:
for i = 1 to n
if i % 15 equals to 0
print "FizzBuzz"
else if i % 3 equals to 0
print "Fizz"
else if i % 5 equals to 0
print "Buzz"
else
print i
Modulo is a costly operator and should be used the minimum number of times. This approach can be optimised using the if statement and a boolean flag variable. The flag variable is used to check if the number is divisible by 3 and 5. If divisible, the next line is printed; else, the number itself is printed.
for i = 1 to n
initialize a flag variable with false value to check if none of the conditions matches
if i % 3 equals to 0
print "Fizz"
set flag to true
if i % 5 equals to 0
print "Buzz"
set flag to true
if flag is true
print endline
else
print i
In this case, the modulo operator is used only two times. Code for the above approach is given below:
A. FizzBuzz program in Java
class Main {
public static void main(String[] args) {
// let n be 15
int n = 15;
for(int i = 1; i <= n ; i++) {
// check if none of the conditions matches
boolean flag = false;
// prints Fizz if divisible by 3
if(i % 3 == 0) {
System.out.print("Fizz");
flag = true;
}
// prints Buzz if divisible by 5
if(i % 5 == 0) {
System.out.print("Buzz");
flag = true;
}
// prints the nextline if divisible by 3, 5, or both
if(flag)
System.out.println();
// prints i if not divisible by 3 and 5
else
System.out.println(i);
}
}
}
B. FizzBuzz program in C++
#include <iostream>
using namespace std;
int main() {
// let n be 15
int n = 15;
for(int i = 1; i <= n ; i++) {
// check if none of the conditions matches
bool flag = false;
// prints Fizz if divisible by 3
if(i % 3 == 0) {
cout<<"Fizz";
flag = true;
}
// prints Buzz if divisible by 5
if(i % 5 == 0) {
cout<< "Buzz";
flag = true;
}
// prints the nextline if divisible by 3, 5, or both
if(flag)
cout<<endl;
// prints i if not divisible by 3 and 5
else
cout<<i<<endl;
}
}
C. FizzBuzz program in Python
# Let n be 15
n = 15
for i in range(1,n+1):
# check if none of the conditions matches
flag = False
# prints Fizz if divisible by 3
if i % 3 == 0:
print("Fizz",end='')
flag = True
# prints Buzz if divisible by 5
if i % 5 == 0:
print("Buzz",end='')
flag = True
# prints the nextline if divisible by 3, 5, or both
if flag:
print()
# prints i if not divisible by 3 and 5
else:
print(i)
- Time Complexity: O(N)
- Space Complexity: O(1)
2. FizzBuzz optimised implementation: Using count variable and assignment operator
- The above approach is not suitable for large numbers. This is because the computational complexity is O(N^2) for the modulo operator, making it slow for large numbers.
- A number is divisible by another number if it is a multiple of the other number. Multiplication is basically the repeated addition of a number. This concept will be used to solve the FizzBuzz program.
- In this approach, a count variable is repeatedly incremented by 1. The divisibility test is then done by comparing the count variable with the number. This eliminates the need for the modulo operator.
- The variables count3 and count5 are incremented by 1 every time the number is not divisible by 3 and 5. If count3 is equal to 3, the number is a multiple of 3. Similarly, the number is a multiple of 5 when count5 is equal to 5. The count variable is set to 0 again when these conditions are met.
- This is an effective way to solve the FizzBuzz program. The count variables will be increased in every iteration, and the divisibility of the number will be verified using the assignment operator.
Pseudo Code:
initialize count3, count5 with 0
for i = 1 to n
increment count3 and count5
initialize a flag variable with false value to check if none of the conditions matches
if count3 equals to 3
print "Fizz"
set flag to true
set count3 to 0
if count5 equals to 5
print "Buzz"
set flag to true
set count5 to 0
if flag is true
print endline
else
print i
Code for the above approach is given below:
A. FizzBuzz program in Java
class Main {
public static void main(String[] args) {
// let n be 15
int n = 15, count3 = 0, count5 = 0;
for(int i = 1; i <= n ; i++) {
count3++;
Count5++;
// check if none of the conditions matches
boolean flag = false;
// prints Fizz if divisible by 3
if(count3 == 3) {
System.out.print("Fizz");
flag = true;
count3 = 0;
}
// prints Buzz if divisible by 5
if(count5 == 5) {
System.out.print("Buzz");
flag = true;
count5 = 0;
}
// prints the nextline if divisible by 3, 5, or both
if(flag)
System.out.println();
// prints i if not divisible by 3 and 5
else
System.out.println(i);
}
}
}
B. FizzBuzz program in C++
#include <iostream>
using namespace std;
int main() {
// let n be 15
int n = 15, count3 = 0, count5 = 0;
for(int i = 1; i <= n ; i++) {
count3++;
count5++;
// check if none of the conditions matches
bool flag = false;
// prints Fizz if divisible by 3
if(count3 == 3) {
cout<<"Fizz";
flag = true;
count3 = 0;
}
// prints Buzz if divisible by 5
if(count5 == 5) {
cout<<"Buzz";
flag = true;
count5 = 0;
}
// prints the nextline if divisible by 3, 5, or both
if(flag)
cout<<endl;
// prints i if not divisible by 3 and 5
else
cout<<i<<endl;
}
}
C. FizzBuzz program in Python
# Let n be 15
n = 15
count3 = 0
count5 = 0
for i in range(1,n+1):
count3 = count3 + 1
count5 = count5 + 1
# check if none of the conditions matches
flag = False
# prints Fizz if divisible by 3
if count3 == 3:
print("Fizz",end='')
flag = True
count3 = 0
# prints Buzz if divisible by 5
if count5 == 5:
print("Buzz",end='')
flag = True
count5 = 0
# prints the nextline if divisible by 3, 5, or both
if flag:
print()
# prints i if not divisible by 3 and 5
else:
print(i)
- Time Complexity: O(N)
- Space Complexity: O(1)
Output for the FizzBuzz Program:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
When n = 15, this is the output obtained.
Variations of the FizzBuzz Program
The FizzBuzz program can be modified in various ways. Some of the variations are:-
- Changing the divisibility number: Any other number other than 3 and 5 can be asked. In that case, change the if conditions accordingly.
- Changing the result on divisibility check: Instead of FizzBuzz, some other results like HelloWorld can be asked. In that case, change the print statement accordingly.
- Replace the number containing the digits 3 and 5:
- Problem Statement: Check the digits of the number and print “Fizz” if the number contains the digit 3, print “Buzz” if the number contains the digit 5, else print the number itself.
- In this individual digits have to be checked. If the number contains 3 print “Fizz” and “Buzz” if the number contains 5.
Pseudo-code:
for i to n
initialize a temp variable with i
initialize result, an empty string
initialize a flag variable with false value to check if none of the conditions matches
while temp > 0
initialize a variable remainder to obtain the last digit and set it to temp % 10
set temp to temp/10
if remainder equals to 3
set result = "Fizz" + result
set flag to true
if remainder equals to 5
set result = "Buzz" + result
set flag to true
if flag is false
set result = remainder + result
print result
Output when n = 15:
1
2
Fizz
4
Buzz
6
7
8
9
10
11
12
Fizz
14
Buzz
4. Replace only the digits 3 and 5 in the number:
Problem Statement: Check the digits of the number and print “Fizz” in the place of 3, print “Buzz” in the place of 5, else print the number itself. In this individual digits have to be checked. If the digit is 3 print “Fizz” only in that and “Buzz” if the digit is 5.
Pseudo-code:
for i to n
initialize a temp variable with i
initialize result, an empty string
while temp > 0
initialize a flag variable with false value to check if none of the conditions matches
initialize a variable remainder to obtain the last digit and set it to temp % 10
set temp to temp/10
if remainder equals to 3
set result = "Fizz" + result
set flag to true
if remainder equals to 5
set result = "Buzz" + result
set flag to true
if flag is false
set result = remainder + result
print result
Output when n = 15:
1
2
Fizz
4
Buzz
6
7
8
9
10
11
12
1Fizz
14
1Buzz
5. Change direction after the occurrence of a number divisible by 3 and 5.
Problem Statement: Initially print “Fizz” for a number divisible by 3 and “Buzz” for a number divisible by 5. Then the words are interchanged when a number divisible by both 3 and 5 is encountered. And this process continues until n is reached.
This can be solved by modifying the count variable approach. Here an additional direction variable will be used along with the flag variable of type int. The flag variable is incremented when a number divisible by 3 or 5 is encountered. When the flag variable becomes two, the direction is changed.
Pseudo-code:
initialize count3, count5 with 0
initialize a boolean variable direction
for i = 1 to n
increment count3 and count5
initialize a flag variable with 0
if count3 equals to 3
if direction is true
print "Fizz"
if direction is false
print "Buzz"
increment flag
set count3 to 0
if count5 equals to 5
if direction is true
print "Buzz"
if direction is false
print "Fizz"
increment flag
set count5 to 0
if flag is not equal to 0
print nextline
else
print i
if flag is equal to 2
Set direction to !direction
Output when n = 18:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Buzz
Frequently Asked Questions
Yes, it is a common question asked in coding tests to judge the basic coding ability of the candidate.
In the FizzBuzz program, the numbers from 1 to n are checked for divisibility by 3 and 5. If the number is divisible by 3: “Fizz” is printed, if divisible by 5: “Buzz” is printed, if divisible by 3 and 5 “FizzBuzz” is printed; else the number itself is printed.
FizzBuzz can be implemented using the modulo operator or the count variable approach.
Modulo operations are time-consuming and are not suitable for large numbers.
For loop and conditional branching is tested using the FizzBuzz program along with the problem-solving ability.
Key Takeaways
This blog attempted to give a detailed explanation of the FizzBuzz Program. The various approaches have been explained with the help of pseudo-code and the implementations in Java, C++, and Python. Some variations of the FizzBuzz program have also been discussed along with the pseudo-code.
If you intend to learn to programme you may want to check out our courses. Also, if you are interested in practising coding problems asked by Amazon, Google, Microsoft or other product based companies, you may check out our CodeStudio platform. Guided Paths are another attraction on the platform!
By Hari Sapna Nair
Leave a Reply