[RAILS] Fizz Buzz (Ruby edition)

Solve Fizz Buzz (Ruby)

problem

The integer N is given as input.

Display integers from 1 to N in order from 1.

However, the number you are trying to display is

・ When it is a multiple of 3 and a multiple of 5, "Fizz Buzz" ・ When it is a multiple of 3, "Fizz" ・ When it is a multiple of 5, "Buzz"

Please display instead of the numerical value.

Value to be entered

The input is given in the following format.

N

N is an integer greater than or equal to 1 and less than or equal to N.

Expected output

At the end, start a new line and do not include extra characters or blank lines.

Input example

20

Output example

1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 Fizz Buzz 16 17 Fizz 19 Buzz

My answer

python


num = gets.to_i
(1..num).each do |n|
    if n % 15 == 0
        puts "Fizz Buzz"
    elsif n % 5 == 0
        puts "Buzz"
    elsif n % 3 == 0
        puts "Fizz"
    else
        puts n
    end
end

The point is that (1..num) takes out the numbers from 1 to num in order with the each method, and when divided by 3, 5, and 15, if it becomes 0, the character string is output. It's very simple, but it can be a tough problem if you don't have a programming brain. Don't forget else!

that's all!

Recommended Posts

Fizz Buzz (Ruby edition)
Pedometer (Ruby edition)
Safe numbers (ruby edition)
Minimum value (ruby edition)
Q. Elevator (Ruby edition)
ruby memorandum (basic edition)
Offline environment construction Ruby edition
Dharma doll drop of characters (ruby edition)
Ruby on Rails installation method [Mac edition]
[Problem] Weather on consecutive holidays (Ruby edition)