Home »
Ruby Tutorial »
Ruby Programs
Ruby program to get the denominator from a rational number
Last Updated : December 15, 2025
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