-
-
Save EfeAgare/6a7ee783ec7320d20349e049f476f0d5 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”.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment