ADVERTISEMENT
ADVERTISEMENT

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 »


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.