Skip to content

Instantly share code, notes, and snippets.

@catermelon
Created October 9, 2013 01:08
Show Gist options
  • Save catermelon/6894560 to your computer and use it in GitHub Desktop.
Save catermelon/6894560 to your computer and use it in GitHub Desktop.
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment