Fizz-Buzz Program in Python

Fizz-Buzz is the programming task used for explaining the division of numbers in the Fizz, Buzz, and Fizz_Buzz group.

Suppose the user has the number 'n,' and they have to display the string representation of all the numbers from 1 to n. But there are some limitations such as:

  • Suppose the number is dividable by 3, then it will print "Fizz" in place of the number.
  • If the number is dividable by 5, it will print "Buzz" in place of the number in the result.
  • And suppose the given number is dividable by both 3 and 5, it will print "Fizz_Buzz" in place of the number.
  • And if the number is neither dividable by 3 nor 5, it will print the number as the string.

For solving the above problem and considering the limitations, we have to follow the below steps:

In the program, we have to apply the condition for all the numbers from 1 to 'n.'

Example:

Output:

['1', '2', ' Fizz ', '4', ' Buzz ', ' Fizz ', '7', '8', ' Fizz ', ' Buzz ', '11', ' Fizz ', '13', '14', ' Fizz_Buzz ', '16', '17', ' Fizz ', '19', ' Buzz ', ' Fizz ', '22', '23', ' Fizz ', ' Buzz ', '26', ' Fizz ', '28', '29', ' Fizz_Buzz ', '31', '32', ' Fizz ', '34', ' Buzz ', ' Fizz ', '37', '38', ' Fizz ', ' Buzz ']

We can also write the above program by using the loop method.

Example:

Output:

	Fizz_Buzz 
1
2
 Fizz
4
 Buzz 
 Fizz
7
8
 Fizz
 Buzz 
11
 Fizz
13
14
 Fizz_Buzz 
16
17
 Fizz
19
 Buzz 
 Fizz
22
23
 Fizz
 Buzz 
26
 Fizz
28
29
 Fizz_Buzz 
31
32
 Fizz
34

Summary

In this article, we have discussed how the user can write a python program for the Fizz Buzz problem, we have also discussed some limitations that must be followed.


Next TopicTabula Python




Latest Courses