Last Updated on January 2, 2023 by Prepbytes
In this article, we are going to study the Fizzbuzz program in python. Right from getting the overview of the famous interview, we will be looking at the dry run as well as the algorithm adapted to solve the question and in the later stages of this article, we will look at the code and the analysis of the fizzbuzz program in python.
What is Fizzbuzz Program in Python
Fizzbuzz is a famous programming problem that is taught to people who are novices in the field of programming. In that case, it becomes one of the important problems to get real-life logic into the application using a programming language.
It is often said as the Game of Counting where the participants have to replace the multiple of three by speaking Fizz, the multiple of 5 by Buzz and the multiple of 3 and 5 both by calling out FizzBuzz. The rest of the numbers should be called as it is with no change. A number is given at the start of the play and the counting in the game must be performed till the number is encountered.
Dry Run of Fizzbuzz Program in Python
Now that we have an idea of some theoretical knowledge related to Fizzbuzz, let us look at the dry run portion before we move on to the fizzbuzz program in python. Suppose a number, i.e. 10 is taken as the point up to which FizzBuzz will be conducted. Tracing the result for each number.
1 and 2 are printed as it is. Fizz is printed in place of 3. 4 is printed. Buzz is printed in place of 5. Fizz is printed in place of 6. 7,8 are printed. Fizz is printed in place of 9. Buzz is printed in place of 10. 11 is printed. Fizz is printed in place of 12. 13,14 are printed. FizzBuzz is printed in place of 15. 16,17 are printed. Fizz is printed in place of 18. 19 is printed. Buzz is printed in place of 20.
Algorithm of Fizzbuzz Program in Python
The above-mentioned flowchart can give a clear idea of the actual fizzbuzz program in python the initial condition on if the number is lesser than the count is followed up with the printing triplet condition resulting in the increment of local count until the local counts exceed the user input.
The algorithm to fizzbuzz is pretty simple and sleek. Whenever we get a multiple of 3 and 5, we print FizzBuzz, a multiple of 3 will result in Fizz and a multiple of 5 will get Buzz printed instead. If none of the conditions is satisfied, the actual number at the index is going to be printed.
The pseudocode of fizzbuzz can be explained in the following manner below:-
- Take N as input from the user
- Initialize i as 1
- Run a loop i till N:
- If i % 5 == 0 and i % 3 == 0 then print “FizzBuzz”
- Elif i % 3 == 0 then print “Fizz”
- Elif i % 5 ==0 then print “Buzz”
- Else print i
- End
Code of Fizzbuzz Program in Python
Approach One: Using Modulo
n = int(input()) for i in range(1,n+1): if i%3==0 and i%5 == 0: print("FizzBuzz") elif i%5 == 0: print("Buzz") elif i%3 == 0: print("Fizz") else: print(i)
Explanation: we are taking n as the input number from the user and later on, a loop is made to run from 1 to n+1 ( internal working of python ) with discussed conditions put in place. Here is an attached code snippet to understand the code better.
Output
20
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Approach 2 – Without Using Modulo
class Main { n = int(input()) count3,count5 = 1,1 for i in range(1,n+1): if count3 == 3 and count5 == 5: print("FizzBuzz") count3,count5 = 0,0 elif count3 == 3: print("Fizz") count3 = 0 elif count5 == 5: print("Buzz") count5= 0 else: print(i) count3 += 1 count5 += 1
Explanation: We are taking n as the input number from the user and later on, setting counter3 and count5 as 1 and 1 respectively, a loop is made to run from 1 to n+1 ( internal working of python ) with increment perform on count3 and count5 on each iteration. Once count5 equals 5, Buzz is printed and count5 is restored to 0, and count3 on equalling 3, Fizz is printed and count3 is restored to 0. In a case when both conditions turn true, FizzBuzz is printed and both the objects’ values are set to 0.
Output
20
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Variation of Fizzbuzz Program in Python
There can be several variations of the fizzbuzz program in python that you might come across in an interview. A few of the variations of this program are discussed as follows:-
Replace containing 3 or 5 – Instead of replacing the multiple, we replace the numbers with fizz, buzz or both depending on the digit the number contains.
Base change – Instead of working on base 10, fizzbuzz can be implemented on a different mathematical base as well.
Replace 3 or 5 in the number – In this variation, we replace 3 with the string “Fizz” and 5 with the string “Buzz” and the rest of the number remains unaltered.
Analysis of the Fizzbuzz Program in Python
Analyzing the time complexity of the program, we are running a loop until the count set for the fizzbuzz program in python, therefore it is going to have a worst-case time complexity, O(n).
Space complexity is going to be O(1) as long as we are not storing the results in a desired container.
Conclusion
Nearing the end of the article, we have so far discussed what fizzbuzz is and how to implement a fizzbuzz program in python along with its dry run, algorithm, code and variations in the program.
Hope you liked this article. Thank you and see you again for another insightful piece of information. Keep learning from PrepBytes.
Other Python Programs
Python program to reverse a number
Python program for heap sort
Python program to check armstrong number
Python program to check leap year
Python program to convert celsius to fahrenheit
Python program to find factorial of a number
Python program to reverse a linked list
Python Program to find the middle of a linked list using only one traversal
Python Program to Add Two Numbers
Python Program to Check Palindrome Number
Python Program to Print the Fibonacci Series
Python Loop Program
Anagram Program in Python