ADVERTISEMENT
ADVERTISEMENT

Ruby program to get the denominator from a rational number

Ruby Example: Write a program to get the denominator from a rational number.
Submitted by Nidhi, on December 15, 2021

Problem Solution:

In this program, we will create three variables and initialize them with rational numbers. Then we will get the denominator from a rational number using the denominator() library function.

Program/Source Code:

The source code to get the denominator from a rational number is given below. The given program is compiled and executed successfully.

# Ruby program to get the denominator 
# from a rational number

num1 = Rational(21, 7);
num2 = Rational(-7, 20);
num3 = Rational(21);

print "Num1 denominator: "  ,num1.denominator();
print "\nNum2 denominator: ",num2.denominator();
print "\nNum3 denominator: ",num3.denominator();

Output:

Num1 denominator: 1
Num2 denominator: 20
Num3 denominator: 1

Explanation:

In the above program, we created three variables num1, num2, num3 and initialized them with rational numbers. Then we got the denominator from the given rational number using the Rational() library function and printed the result.

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.