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
}
@kri178c
Copy link

kri178c commented Feb 15, 2020

is there a way to do it in visual basic?

@cheema98
Copy link

{
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+",");
    }
    }

@Jojokristiadi
Copy link

thz

@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