The fizzbuzz program in python

The FizzBuzz program is a great way to test your Python skills in regards to conditions and loops. The task is to loop through the numbers between 1 and 100, and for each number, check if it is divisible by 3. If it is, print “fizz.” If it is divisible by 5, print “buzz.” If the number is divisible by both 3 and 5, print “fizzbuzz.” For all other numbers, simply print the number itself. This simple program can help you improve your programming abilities and is a great tool for beginners to learn basic programming concepts.

# Algorithm
F(N) = "fizzbuzz" # if N % 3 == 0 && N % 5 == 0
     = "buzz" # if N % 5 == 0
     = "fizz" # if N % 3 == 0
     = N  # for all other cases

fizzbuzz program in python

# fizzbuzz.py
# prints fizz if divisible by only 3
# print buzz if divisible by only 5
# print fizzbuzz if divisible by both 5 and 3.
def fizzBuzzFunc(num):
    if num % (3*5) == 0:
        return 'fizzbuzz'
    elif num % 5 == 0:
        return 'buzz'
    elif num % 3 == 0:
        return 'fizz'
    else:
        return str(num)
    
# fizzbuzz program to print 1 to 100.
for i in range(1, 100):
    print(fizzBuzzFunc(i))

The FizzBuzz program is a common programming exercise that tests a developer’s understanding of control flow and loops by having them write a program that prints the numbers from 1 to 100, but with a twist. The program checks for certain conditions on each number before printing it.

The program starts with a function called fizzBuzzFunc() which takes an argument num which is the number we want to check the conditions on. The function checks whether num is divisible by both 3 and 5, if so, it returns the string ‘fizzbuzz’. If not, it then checks whether num is divisible by 5 and if so, it returns ‘buzz’. If the number is not divisible by 5, it then checks whether num is divisible by 3 and if so, it returns ‘fizz’. If none of the conditions above are met, it returns the number as a string.

The program then uses a for loop to iterate through the numbers from 1 to 100 and for each number, it calls the fizzBuzzFunc() function passing the current number as an argument and prints the output of the function, which is either ‘fizz’, ‘buzz’, ‘fizzbuzz’ or the number itself.

In this way, the program is able to print the numbers from 1 to 100, but with a twist. It checks for certain conditions on each number before printing it and this is a great way to test a developer’s understanding of control flow and loops.

The output of the above program is as follows.

# Output
$python fizzbuzz.py
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

The program runs with a linear (O(N)) time complexity. This is all about the fizzbuzz program in python. Happy learning!.