Open Raashika0201 opened 2 years ago
def fizzbuzz(n): result = ["FizzBuzz" if i % 3 == 0 and i % 5 == 0 else "Fizz" if i % 3 == 0 else "Buzz" if i % 5 == 0 else i for i in range(1, n+1)] return result
n = int(input("Enter the range :")) output = fizzbuzz(n) print(output)
def fizz_buzz(n):
result = []
# Loop from 1 to n
for i in range(1, n + 1):
# Check if i is divisible by both 3 and 5
if i % 3 == 0 and i % 5 == 0:
# Add "FizzBuzz" to the result list
result.append("FizzBuzz")
# Check if i is divisible by 3
elif i % 3 == 0:
# Add "Fizz" to the result list
result.append("Fizz")
# Check if i is divisible by 5
elif i % 5 == 0:
# Add "Buzz" to the result list
result.append("Buzz")
else:
# Add the current number as a string to the
# result list
result.append(str(i))
# Return the result list
return result
n = 100
result = fizz_buzz(n)
print(' '.join(result))
Write a Python program that iterates the integers from 1 to 50. For multiples of three print "Fizz" instead of the number and for multiples of five print "Buzz". For numbers that are multiples of three and five, print "FizzBuzz".
Solution:
Python Code : for fizzbuzz in range(51): if fizzbuzz % 3 == 0 and fizzbuzz % 5 == 0: print("fizzbuzz") continue elif fizzbuzz % 3 == 0: print("fizz") continue elif fizzbuzz % 5 == 0: print("buzz") continue print(fizzbuzz)
Sample Output: fizzbuzz 1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz .....
41 fizz 43 44 fizzbuzz 46 47 fizz 49 buzz Flowchart:
Flowchart: Python program to get Fizz, Buzz and FizzBuzz