Ruby program to check a given number is finite or not

Ruby Example: Write a program to check a given number is finite or not.
Submitted by Nidhi, on December 14, 2021

Problem Solution:

In this program, we will create 2 variables and initialize them with some values. Then we will check given number is finite or not using the built-in finite?() function.

Program/Source Code:

The source code to check a given number is finite or not is given below. The given program is compiled and executed successfully.

# Ruby program to check a given number 
# is finite or not

num1 = 10;
num2 = 1/0.0;

if num1.finite?()
    print "num1 is a finite number\n";
else
    print "num1 is an infinite number\n";
end

if num2.finite?()
    print "num2 is a finite number\n";
else
    print "num2 is an infinite number\n";
end

Output:

num1 is a finite number
num2 is an infinite number

Explanation:

In the above program, we created two variables num1, num2 and initialized 10, 1/0.0 respectively. Then we used finite?() function to check given number is a finite number or not and print the appropriate message.

Ruby Basic Programs »




Comments and Discussions!

Load comments ↻





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