Q:

Ruby program to demonstrate the downto() function

belongs to collection: Ruby Basic Programs

0

In this program, we will demonstrate the downto() function. The downto() function returns all the numbers less than equal to the number till the specified number.

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 demonstrate the downto() function is given below. The given program is compiled and executed successfully.

# Ruby program to demonstrate the 
# downto() function

num1 = 5 ;
num2 = -5;

# Prints the number down to 1
num1.downto(1){| i | print i, " "}

print "\n";

# Prints the number down to -10
num2.downto(-10){| i | print i, " "}

Output:

5 4 3 2 1 
-5 -6 -7 -8 -9 -10

Explanation:

In the above program, we created two integer variables num1num2 that are initialized with 5, -5 respectively. Then we called the downto() function and printed numbers in decreasing order till the specified number.

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 upto() function... >>
<< Ruby program to print ASCII value of given charact...