×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby program to check the given number is an odd number using library function

Last Updated : December 15, 2025

Problem Solution

In this program, we will create two variables and initialize them with some values. Then we will find the created variables contain an ODD number or not using library function odd?().

Program/Source Code

The source code to check the given number is an odd number using the library function is given below. The given program is compiled and executed successfully.

# Ruby program to check given number is an 
# odd number using library function

num1 = 20
num2 = 3

if num1.odd?()
    print "num1 is an odd number\n";
else
    print "num1 is an even number\n";
end

if num2.odd?()
    print "num2 is an odd number\n";
else
    print "num2 is an even number\n";
end

Output

num1 is an even number
num2 is an odd number

Explanation

In the above program, we created two variables num1, num2 and initialized 20, 3 respectively. Then we used the odd?() function to check created variables contains the odd number or not and printed the appropriate message. The odd?() function returns a Boolean value. It returns true when the variable contains an ODD number, otherwise, it returns false.

Ruby Basic Programs »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.