Ruby program to get the numerator from a rational number

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

Problem Solution:

In this program, we will create 3 variables and initialize them with the rational number. Then we will use the numerator() function to get numerator from rational numbers.

Program/Source Code:

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

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

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

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

Output:

Num1 numerator: 3
Num2 numerator: -7
Num3 numerator: 21

Explanation:

In the above program, we created three variables and initialized them with the rational number. Then we used the numerator() function to get the numerator from rational numbers and printed the result.

Ruby Basic Programs »





Comments and Discussions!

Load comments ↻






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