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.)

    def even_or_odd(number)
    if number = (0,2)
    prints even
    else
    if number = (-1,1,7)
    prints odd
    end

    puts (number)

    Does this make sense?



  • 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.



  • 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.


Log in to reply