Created
October 11, 2012 05:21
-
-
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”.
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 | |
} |
{
for(int i = 0;i<100;i++){
if (i%3==0 && i%5==0)
{System.out.println("FooBar"+", ");
}else if(i%3==0){
System.out.println("Foo"+",");
}
else if (i%5==0){ System.out.println("Bar"+", ");
}
else{
System.out.println(i+",");
}
}
thz
wow
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is there a way to do it in visual basic?