Skip to content

Instantly share code, notes, and snippets.

@rubyginner
Created October 11, 2012 05:21
Show Gist options
  • Save rubyginner/3870360 to your computer and use it in GitHub Desktop.
Save rubyginner/3870360 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 “Foo” instead of the number and for the multiples of five print “Bar”. For numbers which are multiples of both three and five print “FooBar”.
# version 1
for counter in 1..100
if (counter % 3 == 0) && (counter % 5 == 0)
puts 'FooBar'
elsif (counter % 3 == 0)
puts 'Foo'
elsif (counter % 5 == 0)
puts 'Bar'
else
puts counter
end
end
# version 2
counter = 1
while counter <= 100
if (counter % 3 == 0) && (counter % 5 == 0)
puts 'FooBar'
elsif (counter % 3 == 0)
puts 'Foo'
elsif (counter % 5 == 0)
puts 'Bar'
else
puts counter
end
counter += 1
end
# nerd version
(1..100).each { |number|
op = []
if number%3==0
op.push("Foo")
end
if number%5==0
op.push("Bar")
end
puts op.size > 0 ? op.join("") : number.to_s
}
@voguegia
Copy link

voguegia commented May 6, 2022

wow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment