Skip to content

Instantly share code, notes, and snippets.

@suryart
Last active August 24, 2016 19:32
Show Gist options
  • Save suryart/6561608 to your computer and use it in GitHub Desktop.
Save suryart/6561608 to your computer and use it in GitHub Desktop.
Write a program which prints the numbers from 1 to N, each on a new line. But for multiples of three print “Fizz” instead of the number 3 and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”. Read in the input number from STDIN.Solution below is in ruby.
1.upto(STDIN.gets.to_i) do |num|
break if num > 10**7 # num should be less than 10^7
output = nil
output = output.to_s + 'Fizz' if num % 3 == 0
output = output.to_s + 'Buzz' if num % 5 == 0
puts output || num
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment