dubu2 dubu2 - 2 months ago 9
Ruby Question

Ruby function to check if a number is divisible by five and is even

def is_even?(n)
remainder_when_divided_by_2 = n % 2

if remainder_when_divided_by_2 == 0
return true
else
return false
end
end

def is_odd?(n)
return ! is_even?(n)
end

puts "1 is_even? #{is_even?(1)} - is_odd? #{is_odd?(1)}"
puts "2 is_even? #{is_even?(2)} - is_odd? #{is_odd?(2)}"
puts "3 is_even? #{is_even?(3)} - is_odd? #{is_odd?(3)}"
puts "4 is_even? #{is_even?(4)} - is_odd? #{is_odd?(4)}"
puts "5 is_even? #{is_even?(5)} - is_odd? #{is_odd?(5)}"
puts "6 is_even? #{is_even?(6)} - is_odd? #{is_odd?(6)}"

def is_even_and_divisible_by_five?(n)
remainder_when_divided_by_five = n % 5

if (remainder_when_divided_by_five == 0) && (is_even?(n) == true)
return true
else
return false
end

end

puts "5 is_even_and_divisible_by_five? #{is_even_and_divisible_by_five?(5)}"
puts "10 is_even_and_divisible_by_five? #{is_even_and_divisible_by_five?(10)}"
puts "15 is_even_and_divisible_by_five? #{is_even_and_divisible_by_five?(15)}"
puts "20 is_even_and_divisible_by_five? #{is_even_and_divisible_by_five?(20)}"
puts "25 is_even_and_divisible_by_five? #{is_even_and_divisible_by_five?(25)}"
puts "30 is_even_and_divisible_by_five? #{is_even_and_divisible_by_five?(30)}"


The problem was I had not called the method is_even_and_divisible_by_five in the puts commands at the bottom of the code. I called it is_even_and_divisble_by_5. Then in the if statement in the is_even_and_divisble_by_five method, I left of the (n) arguement from Is_even. Thank you all very much!

Answer

Even (divisible by two) and divisible by five also means "divisible by ten":

def is_even_and_divisible_by_five?(n)
  n % 10 == 0
end