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")

 

3 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. Fabulous website!!! I'm a cs student who is learning Python and this website is really really very helpful!

    ReplyDelete
  2. btw the code is slightly incorrect as if it sees a multiple of both 3 and 5 it will print
    fizz
    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

    ReplyDelete
    Replies
    1. This code will work better:

      for 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)

      Delete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post