WebMasterCampus
WEB DEVELOPER Resources

Fizzbuzz Example using Python

Fizzbuzz Example using Python


Fizz Buzz is a basic programming task that usually asked in interview questions for junior programmer or developer job role.

FizzBuzz Program Requirement

  1. Write a program that prints the numbers from 1 to 100.
  2. For multiples of 3 print “Fizz” instead of the number.
  3. For the multiples of 5 print “Buzz”.
  4. Please note 15 is the multiple of 3 and 5 both so, if a number is multiple of 3 and 5 both then print “FizzBuzz”.

FizzBuzz Example 1


for i in range (1 , 101):

    if i % 3 == 0 and i % 5 == 0:
        print("fizz buzz")
    elif i % 3 == 0:
        print("fizz")
    elif i % 5 == 0:
        print("buzz")
    else:
        print(i)

FizzBuzz Example 2


count_3 = 0
count_5 = 0
message = ""

for i in range (1, 101):

    count_3 += 1
    count_5 += 1

    if count_3 == 3:
        message += "Fizz"
        count_3 = 0
    if count_5 == 5:
        message += "Buzz"
        count_5 = 0

    if message == "":
        print(i)
    else:
        print(message)

    message = ""

Output

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz
Created with love and passion.