Q:

Ruby program to check a given number is finite or not

belongs to collection: Ruby Basic Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 num1num2 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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to get the real part from a Complex n... >>
<< Ruby program to demonstrate the phase() function...