The FizzBuzz algorithm is one of the favourite questions in coding interviews. Fizz and Buzz refer to any number that is a multiple of 3 and 5. In this article, I will walk you through how to implement the FizzBuzz algorithm using C++ and Python programming language.
FizzBuzz Algorithm
The FizzBuzz algorithm comes from a children’s game. This algorithm has been one of the favourite coding interview questions for a very long time. In this problem, you are given a range of integers and you need to produce output according to the rules mentioned below:
- If the integer (x) is divisible by 3, the output must be replaced by “Fizz”.
- If the integer (x) is divisible by 5, the output must be replaced by “Buzz”.
- If the integer (x) is divisible by 3 and 5, the output should be replaced by “FizzBuzz”.
This coding problem is popular among numbers 3 and 5, but you may be able to see more complex numbers, but the logic for solving the problem will remain the same.
Also, Read – Python Projects with Source Code: Solved and Explained.
FizzBuzz Algorithm using C++ and Python
In this section, I’ll walk you through how to implement the FizzBuzz algorithm using C++ and Python programming language. Let’s start by implementing it using C++:
Output:
1
2
Fizz
4
Buzz
Fizz
7
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Generally, it is preferred to use only C++ and Java programming languages for solving problems of Data Structures and algorithms, but still below is the implementation of this algorithm using Python as it is so popular it will help you to implement the concept of Data Structures and Algorithms in Python projects. Now let’s see how to implement the FizzBuzz algorithm using Python:
Output: 1 2 Fizz 4 Buzz Fizz 7 Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19
Summary
Fizz and Buzz refer to numbers that are divisible by 3 and 5. If a number is divisible by 3, it is replaced by “Fizz”, if the number is divisible by 5, it is replaced by “Buzz”, and if the number is divisible by 3 and 5 then the number is replaced by “FizzBuzz”.
I hope you liked this article on the implementation of FizzBuzz algorithm using C++ and Python programming language. Feel free to ask your valuable questions in the comments section below.