Q:

Ruby program to get the numerator from a rational number

belongs to collection: Ruby Basic Programs

0

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.

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

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 demonstrate the <=> operator... >>
<< Ruby program to demonstrate the inspect() function...