Q. Write a program that checks in the range 1….100 and prints “Fizz” if the numbers multiple of 3 and prints "Buzz" if the number is multiple of 5. It should print “FizzBuzz” if the number multiple of both 3 and 5.
Answer =
for i in range (1, 101) : if i % 3 == 0 : print ("Fizz") if i % 5 == 0 : print ("Buzz") if i % 3 == 0 and i % 5 == 0 : print ("Fizz Buzz")
Fabulous website!!! I'm a cs student who is learning Python and this website is really really very helpful!
ReplyDeletebtw the code is slightly incorrect as if it sees a multiple of both 3 and 5 it will print
ReplyDeletefizz
buzz
fizzbuzz
so i suppose you should first put the condition of being a multiple of both 3 and 5 and then later 3 and 5 by themselves as elif statements to make it work
This code will work better:
Deletefor i in range (1, 101) :
if i % 3 == 0 and i% 5 != 0:
print ("Fizz")
elif i % 5 == 0 and i% 3 != 0 :
print ("Buzz")
elif i % 3 == 0 and i % 5 == 0 :
print ("Fizz Buzz")
else:
print(i)
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )