QP School

Full Version: Checking if a number is odd or even in Ruby
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Example 1.
Let's see if 1 is even by executing the following code:


Quote:puts 1.even?


This will print "false" on the screen, because 1 is not even.

Example 2.
Let's see if 2 isĀ even by executing the following code:


Quote:puts 2.even?


This will print "true" on the screen, because 2 is even.

Example 3.
Let's see if 1 is odd by executing the following code:


Quote:puts 1.odd?


This will print "true" on the screen, because 1 is odd.

Example 4.
Let's see if 2 is odd by executing the following code:


Quote:puts 2.odd?


This will print "false" on the screen, because 2 is not odd.