How to verify odd and even numbers

I am trying to write a program that allows the user to input a number,
and prints if the number was odd and even that they entered. I can’t
really find anything online about odd and even functions besides the
int_odd_p function. This is what I have so far.

puts" input a value"
Number1 = STDIN.gets
Number1.chop!

From here I know its a simple if then statement for num1, but I don’t
know how to make that statement clarify if the number is odd or even.

Ruby’s pretty straightforward:

irb(main):001:0> 1.even?
=> false
irb(main):002:0> 2.even?
=> true
irb(main):003:0> 1.odd?
=> true
irb(main):004:0> 2.odd?
=> false

You’ll want to make sure your input is an integer first, though:

number = Integer(gets.chomp)

And an uppercase letter at the start of a variable name makes it a
constant, so you should use lowercase variable names.

Joel P. wrote in post #1169408:

Ruby’s pretty straightforward:

irb(main):001:0> 1.even?
=> false
irb(main):002:0> 2.even?
=> true
irb(main):003:0> 1.odd?
=> true
irb(main):004:0> 2.odd?
=> false

You’ll want to make sure your input is an integer first, though:

number = Integer(gets.chomp)

And an uppercase letter at the start of a variable name makes it a
constant, so you should use lowercase variable names.

Thank you so much for your input but I am using eclipse, and not the
irb.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs