Write a short program that prints each number from 1 to 100 on a new line.
For each multiple of 3, print "Fizz" instead of the number.
For each multiple of 5, print "Buzz" instead of the number.
For numbers which are multiples of both 3 and 5, print "FizzBuzz" instead of the number.
Method 1: (The Obvious One)
n=[n for n in range(1,101)]
for i in n:
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)
Method 2: (Using 1 print statement)
k=[]
n=[n for n in range(1,101)]
for i in n:
if i%3==0 and i%5==0:
k.append("FizzBuzz")
elif i%3==0:
k.append("Fizz")
elif i%5==0:
k.append("Buzz")
else:
k.append(i)
print(k)
Method 3: (Using only two lines of code) //My Favorite One
n=["FizzBuzz" if (n%3==0 and n%5==0) else "Fizz" if (n%3==0) else "Buzz" if(n%5==0) else n for n in range(1,101) ]
print(n)
0 Comments