public
Created

  • Download Gist
fizzbuzz.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 
# Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz"
# instead of the number and for the multiples of five print "Buzz". For numbers which are
# multiples of both three and five print "FizzBuzz".
 
# This is a moldly oldie in the programmer community, it's known as "FizzBuzz" (for obvious
# reasons). Here's a blog post:
# http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html
# This solution runs through every number between 1 and 100, checks if they are evenly
# divisible by five, three, or both five and three, and prints the appropriate thing.
#
# range function: http://docs.python.org/2/library/functions.html#range
# % is the modulo operator. It divides the value of one expression by the value of another,
# and returns the remainder.
 
for n in range(1, 101):
if n % 5 == 0 and n % 3 == 0:
print "FizzBuzz"
elif n % 3 == 0:
print "Fizz"
elif n % 5 == 0:
print "Buzz"
else:
print n

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.