Skip to content

Instantly share code, notes, and snippets.

@carlsednaoui
Last active December 15, 2015 02:09
Show Gist options
  • Save carlsednaoui/5185164 to your computer and use it in GitHub Desktop.
Save carlsednaoui/5185164 to your computer and use it in GitHub Desktop.
Example of FizzBuzz
# Write a program that prints out the numbers 1 to 100 (inclusive).
# If the number is divisible by 3, print Fizz instead of the number.
# If it's divisible by 5, print Buzz. If it's divisible by both 3 and 5, print FizzBuzz.
# Final Version
puts (1..100).collect{ |i| (i % 3 == 0 && i % 5 == 0) ? "FizzBuzz" : (i % 5 == 0) ? "Buzz" : (i % 3 == 0) ? "Fizz" : i}.join("\n")
# Version 1
1.upto(100) do |i|
if i % 3 == 0 && i % 5 == 0
puts "FizzBuzz"
elsif i % 5 == 0
puts "Buzz"
elsif i % 3 == 0
puts "Fizz"
else
puts i
end
end
# Version 2
(1..100).each do |i|
puts (i % 3 == 0 && i % 5 == 0) ? "FizzBuzz" : (i % 5 == 0) ? "Buzz" : (i % 3 == 0) ? "Fizz" : i
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment