Fizz Buzz Python
Fizz Buzz is a classic coding interview question that goes like this:
For numbers from 1 to n:
- If the number is divisible by 3, print “Fizz.”
- If the number is divisible by 5, print “Buzz.”
- If the number is divisible by both 3 and 5, print “FizzBuzz.”
- Otherwise, print the number itself.
Here’s a Python implementation of the Fizz Buzz problem:
def fizz_buzz(n):
for i in range(1, n+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)
# Test the function with n=15
fizz_buzz(15)
When you run the code, it will print the following output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
This code goes through numbers from 1 to 15 and prints the appropriate strings according to the Fizz Buzz rules. You can change the n
parameter in the fizz_buzz
function to test with different ranges of numbers.
Python Training Demo Day 1
Conclusion:
Unogeeks is the No.1 IT Training Institute for Python Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Python here – Python Blogs
You can check out our Best In Class Python Training Details here – Python Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks