ADVERTISEMENT
ADVERTISEMENT

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

Ruby Example: Write a program to check the given number is an EVEN number using library function.
Submitted by Nidhi, on December 12, 2021

Problem Solution:

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

Program/Source Code:

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

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

num1 = 20
num2 = 3

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

if num2.even?()
    print "num2 is an EVEN number\n";
else
    print "num2 is an ODD 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 even?() function to check created variables contains the EVEN number or not and printed the appropriate message. The even?() function returns a Boolean value. It returns true when the variable contains an EVEN number, otherwise, it returns false.

Ruby Basic Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.