Determine if Integer is Even or Odd in Ruby
(Create a function (or write a script in Shell) that takes an integer as an argument and returns "Even" for even numbers or "Odd" for odd numbers.)
if number = (0,2)
if number = (-1,1,7)
Does this make sense?
avan last edited by
When it comes to even and odd there is always a good chance you will use the mod % operator.
Modulus (%)− Divides left hand operand by right hand operand and returns remainder.
So if you want to determine if a number is even or odd simply check if the number % 2 is 0 like so
if number % 2 == 0 # Its even else #Its odd end
I hope that makes sense.