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”.
for n in range(1, 101): line = "" if n%3 == 0: line = line + "Fizz" if n%5 == 0: line = line + "Buzz" if line: print line else: print n
See original on c2.com
An example of Fizz Buzz Test written from the ground up using Test-Driven Development and Ruby:
YOUTUBE CHTep2zQVAc Published on May 3, 2014.
"lorond" offers this version where b and e are indices of substring we need to get, b stands for "beginning" and e stands for "ending"
for (var i = 1; i <= 100; i++) { var b = i % 3 == 0 ? 0 : 4; var e = i % 5 == 0 ? 8 : 4; Console.WriteLine(b == e ? i.ToString() : "FizzBuzz".Substring(b, e - b)); }