Ruby Tutorial – Object Methods Solution

In this post, we will solve Ruby Tutorial – Object Methods HackerRank Solution. This problem (Ruby Tutorial – Object Methods) is a part of HackerRank Ruby series.

Task

Each object in Ruby may have methods associated with it. To demonstrate this, we want you to print whether a number is even or odd. A number has an even? method associated with it, which returns true or false based on the parity of the number.

Assuming a variable number is already defined, check whether a given number is even or not.

Hint

Type in the code-editor

return number.even?

or

number.even?

Input Format

The first line of input contains an integer n. The next n contains one integer in each line.

Output Format

The output is handled by the code written in the editor.

Solution – Ruby Tutorial – Object Methods Solution

def odd_or_even(number)

    return number.even?
    
end

(0...gets.to_i).each do |i|
    puts odd_or_even(gets.to_i)
end

Note: This problem (Ruby Tutorial – Object Methods) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *