FizzBuzz Algorithm and Its Code in Python

The FizzBuzz algorithm is a common coding interview topic. Fizz and Buzz are numbers that are multiples of three and five, respectively.

In this article, we see how to develop the FizzBuzz algorithm using Python.

FizzBuzz Algorithm

A children’s game inspired the FizzBuzz algorithm. This method has long been one of the most popular coding interview tasks.

You are given a range of numbers and must generate output using the following rules:

  • If the given number is divisible by three, the output must be “Fizz.”
  • If the given number is divisible by five, the output must be “Buzz.”
  • If the given number is divisible by three and five, the output must be “FizzBuzz.”

This coding challenge is commonly encountered with digits 3 and 5, but the principle for solving the problem stays the same.

Python Program for FizzBuzz Algorithm

Method #1:Using For loop(Static Input)

Approach:

  • Give the number (upper limit) as static input and store it in a variable.
  • Loop from 1 to the given number using the for loop.
  • Check if the iterator value is divisible by 3 and 5 using the modulus operator and if conditional statement.
  • If it is true, then print “FizzBuzz”.
  • Again Check if the iterator value is divisible by 3 using the modulus operator and elif conditional statement.
  • If it is true, then print “Fizz”.
  • Check if the iterator value is divisible by 5 using the modulus operator and elif conditional statement.
  • If it is true, then print “Buzz”.
  • Else print the iterator value.
  • The Exit of the Program.

Below is the implementation:

# Give the number(upper limit) as static input and store it in a variable.
gvn_numb = 8
# Loop from 1 to the given number using the for loop.
for itr in range(1, gvn_numb+1):
    # Check if the iterator value is divisible by 3 and 5 using the modulus operator
        # and if conditional statement.
    if itr % 3 == 0 and itr % 5 == 0:
        # If it is true, then print "FizzBuzz".
        print(itr, " - FizzBuzz")
    # Again Check if the iterator value is divisible by 3 using the modulus operator
        # and elif conditional statement.
    elif itr % 3 == 0:
        # If it is true, then print "Fizz".
        print(itr, " - Fizz")
    # Check if the iterator value is divisible by 5 using the modulus operator
        # and elif conditional statement.
    elif itr % 5 == 0:
        # If it is true, then print "Buzz".
        print(itr, " - Buzz")
    else:
        # Else print the iterator value.
        print(itr)

Output:

1
2
3  - Fizz
4
5  - Buzz
6  - Fizz
7
8

Method #2:Using For loop(User Input)

Approach:

  • Give the number (upper limit) as user input using the int(input()) function and store it in a variable.
  • Loop from 1 to the given number using the for loop.
  • Check if the iterator value is divisible by 3 and 5 using the modulus operator and if conditional statement.
  • If it is true, then print “FizzBuzz”.
  • Again Check if the iterator value is divisible by 3 using the modulus operator and elif conditional statement.
  • If it is true, then print “Fizz”.
  • Check if the iterator value is divisible by 5 using the modulus operator and elif conditional statement.
  • If it is true, then print “Buzz”.
  • Else print the iterator value.
  • The Exit of the Program.

Below is the implementation:

# Give the number(upper limit) as user input using the int(input()) function 
# and store it in a variable.
gvn_numb = int(input("Enter some random number = "))
print("The output is :")
# Loop from 1 to the given number using the for loop.
for itr in range(1, gvn_numb+1):
    # Check if the iterator value is divisible by 3 and 5 using the modulus operator
        # and if conditional statement.
    if itr % 3 == 0 and itr % 5 == 0:
        # If it is true, then print "FizzBuzz".
        print(itr, " - FizzBuzz")
    # Again Check if the iterator value is divisible by 3 using the modulus operator
        # and elif conditional statement.
    elif itr % 3 == 0:
        # If it is true, then print "Fizz".
        print(itr, " - Fizz")
    # Check if the iterator value is divisible by 5 using the modulus operator
        # and elif conditional statement.
    elif itr % 5 == 0:
        # If it is true, then print "Buzz".
        print(itr, " - Buzz")
    else:
        # Else print the iterator value.
        print(itr)

Output:

Enter some random number = 15
The output is :
1
2
3 - Fizz
4
5 - Buzz
6 - Fizz
7
8
9 - Fizz
10 - Buzz
11
12 - Fizz
13
14
15 - FizzBuzz