Fizzbuzz Program in Python
What is FizzBuzz Program?
The FizzBuzz program is a straightforward application that gives the output as a series of numbers, substituting multiples of 3 with "Fizz," multiples of 5 with "Buzz," and multiples of both 3 and 5 with "FizzBuzz." FizzBuzz program is a common programming challenge used as an interview question for jobs in software development.
How to implement FizzBuzz program in Python?
The FizzBuzz program is quite easy to implement in Python. It entails iterating through a set of numbers in a for loop and determining whether a number is divisible by 3, 5, or both using an if-else expression.
Here is the Python code for the FizzBuzz program:
Example:
for i in range(1, 10): if i % 3 == 0 and i % 5 == 0: print("FizzBuzz") elif i % 3 == 0: print("Fizz") elif i % 5 == 0: print("Buzz") else: print(i)
Output:
1 2 Fizz 4 Buzz Fizz 7 8 Fizz
In this example, a series of numbers from 1 to 10 are produced using the range function. Then, we examine each integer to see if it is divisible by 3, 5, or both using the if-else expression. We print "FizzBuzz" if an integer is divisible by 3 and 5. We print "Fizz" if the number is only divisible by three. If the number is only divisible by 5, "Buzz" is printed. If it cannot be divided by 3 or 5, we just print the number.
Understanding the FizzBuzz code in Python
Let's break down the FizzBuzz code in Python and understand how it works.
for i in range(1, 10)
Using this line of code, a loop from 1 to 10 numbers is iterated through. The loop variable i is what each integer in the range represents.
if i % 3 == 0 and i % 5 == 0: print("FizzBuzz")
This line of code determines whether the integer i may be divided by both 3 and 5. If so, the string "FizzBuzz" is printed.
elifi % 3 == 0: print("Fizz")
Here, the i integer is checked to see if it can be divided by 3 but not by 5. If so, the word "Fizz" is printed.
elif i % 5 == 0: print("Buzz")
The i integer is checked to see if it can be divided by 5 but not by 3. If so, we print the word "Buzz" in the string.
else: print(i)
If none of the aforementioned conditions are true, this line of code is run. It only displays the letter "i."
Variations of FizzBuzz program
The FizzBuzz program can be implemented in Python in a variety of ways. Here are a few illustrations:
Reverse FizzBuzz: The Reverse FizzBuzz program is a modification of the original FizzBuzz program in which we start from the given number and iterate down to 1 rather than beginning from 1 and iterating up to a given number. In this instance, we substitute "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for multiples of both 3 and 5.
Here is the Python code for Reverse FizzBuzz:
n = int(input("Enter a number: ")) for i in range(n, 0, -1): if i % 3 == 0 and i % 5 == 0: print("FizzBuzz") elif i % 3 == 0: print("Fizz") elif i % 5 == 0: print("Buzz") else: print(i)
Output:
Enter a number: 5 Buzz 4 Fizz 2 1
In this code, we begin by requesting a number from the user. The range function is then used to repeat with a step of -1 from that number down to 1. We then decide whether to print "Fizz", "Buzz", "FizzBuzz", or the number itself using the same if-else statements as in the initial FizzBuzz program.
FizzBuzz with User Input Range: In this variant of the FizzBuzz program, we request a range of integers from the user and iterate through that range, substituting "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for multiples of both 3 and 5.
Here is the Python code for FizzBuzz with user input range:
start = int(input("Enter the starting number: ")) end = int(input("Enter the ending number: ")) for i in range(start, end+1): if i % 3 == 0 and i % 5 == 0: print("FizzBuzz") elif i % 3 == 0: print("Fizz") elif i % 5 == 0: print("Buzz") else: print(i)
Output:
Enter the starting number: 5 Enter the ending number: 10 Buzz Fizz 7 8 Fizz Buzz
In this example, we begin by requesting the user's input for the range's beginning and finishing numbers. Then, after iterating through that range using the range function, we use the same if-else logic as in the original FizzBuzz program to decide whether to display "Fizz", "Buzz", "FizzBuzz", or the actual number.