FizzBuzz Program in Python
Overview
FizzBuzz is a common first-level interview question in computer programming that weeds out anyone who cannot program in the desired language. In the Fizz, Buzz, and Fizz Buzz groups, the programming task Fizz-Buzz explains the division of numbers.
Introduction to Fizzbuzz Program in Python
In the Fizz, Buzz, and Fizz Buzz groups, the programming assignment Fizz-Buzz demonstrates the division of numbers. Assume the user is given the number 'n,' and they are asked to display the string representations of all the numbers from 1 to n. However, there are some restrictions, such as:
- If the number can be divided by 3, it will output Fizz instead of the number.
- If the number is divisible by 5, the result will display Buzz instead of the number.
- And if the given number is divisible by both 3 and 5, Fizz Buzz will be printed instead of the number.
- If the number cannot be divided by 3 or 5, it will be printed as a string.
Example to Show Python Program for the Fizz Buzz
Input: Take the numbers as input from the user, separated by commas (",").
1,2,3...100
Output:
1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz…
So as you can see in the above output that where there is a number which is multiple of 3 the program prints "Fizz", prints "Buzz" at the place of a number which is a multiple of 5 and prints "FizzBuzz" at the place of number which is multiple of 3 and 5 both.
Explanation:
- 1 and 2 are not the multiples of 3 or 5, so they both will be printed as it is.
- 3 is a multiple of 3, so Fizz will be printed.
- 4 is not a multiple of 3 or 5, so it will be printed as it is.
- 5 is a multiple of 5, so Buzz will be printed in place of 5.
- 6 is a multiple of 3, so Fizz will be printed.
- 7 and 8 are not the multiples of 3 or 5, so they both will be printed as it is.
- 9 is a multiple of 3, so Fizz will be printed.
- Because 10 is a multiple of five, we print Fizz.
- 11 is not the multiple of 3 or 5, so it will be printed as it is.
- 12 is a multiple of 3, so Fizz will be printed.
- 13 and 14 are not the multiples of 3 or 5, so they both will be printed as it is.
- Because 15 is a multiple of 3 and 5, FizzBuzz will be printed in place of 15. and so on...
Implementation in Python
We'll use a for-in-range loop to solve the fizzbuzz problem in python. We use a for-in-range loop to traverse numbers from 1 to 100 in the code snippet below.
Output :
Note- 101 is used as the end limit in the for a loop because in python the for loop will not include the last element, so that it will go up to 100 only.
Check if the number is divisible by 3 or 5 or both using if-elif-else.
- As shown in the code above, check both numbers and then check each number individually.
- Print the number itself if the number is not divisible by 3 or 5.
Conclusion
- FizzBuzz program is fully explained in this article.
- An example is also explained with full implementation for better understanding.
- So it is clear from the example that if the number is divisible by 3 then the program prints "Fizz".
- If the number is divisible by 5, then the program prints "Buzz"; if the number is divisible by both 3 and 5, then it prints "FizzBuzz".
- If the number is neither divisible by 3 nor by 5, then the program prints the number as it is.