Loops with Ranges

The range as a sequence with .. and ...

The range is used to generate a sequence of values. It requires a start point and an endpoint to specify the values. There are two kinds of ranges:

  1. The range specified with .. generates the values, including the upper limit. For example, 0..100 generates values 00 to 100100.
Press + to interact
Range including the upper limit
Range including the upper limit
  1. The range specified with ... generates the values excluding the upper limit. For example, 0...100 generates values 00 to 9999.
Press + to interact
Range excluding the upper limit
Range excluding the upper limit

It also makes it easy to specify the increment or jump value using the step function. For example, 10..100.step(2) generates even values between 1010 and 100100.

Loop with .. and ...

Let’s say we want to produce a program that counts from 11 to 100100. One way to do this is with the help of a range, as follows:

Press + to interact
for a in 1..100 # Using range in for loop
print(a, "\n")
end

The 1..100 sequence produces integers from 11 to 100100. It works exactly the same as listing these values in square brackets separated by commas.

Let’s take another example of generating the first 100100 non-negative even numbers: 00, 22, 44 ...... 198198.

Press + to interact
for a in 0...100 # Using range in for loop
e = a * 2 # Multiple each value of a with 2
print(e, "\n")
end

In this program, ... generates values from 00 to 9999, and we multiply each value by 22, which displays 0*2, 1*2, 2*299*2.

We may generate even numbers with the same sequence as above using the built-in step function with .... We can directly use the (0...100).step(2), as shown in the following program:

Press + to interact
for a in (0...200).step(2) # Using range function with step parameter
print(a, "\n")
end

If we want to generate the multiples of 7 between 1 and 700, we can use (7...701).step(7). We use 7 as starting value so that every increment of 7 may take us to the next multiple of 7. Incorporating this in the for loop will allow us to show the generated values as output.

Programming practice for .. and ... in loops

The following are a few examples to help you practice writing the programs in Ruby using the range. By clicking the “Show Solution” button, you’ll find the program that solves the respective problem. You may copy and paste the given solution into the code widget to make sure your solution matches the given solution. There can be several ways to write correct solutions in programming.

Odd numbers

Write a program that prints the first 100 positive odd numbers 1,3,5...1991,3,5 ... 199.

Press + to interact
# Write your code here

Arithmetic sequence

Write a program that prints the terms of the sequence 1,6,11...<1001, 6, 11 ... < 100.

Press + to interact
# Write your code here

Table of nn

Write a program that takes an integer input by the user and prints a multiplication table for the first 20 multiples, as illustrated below:

Sample input

5

Sample output

5 x 1 = 5

5 x 2 = 10

.

.

.

5 x 20 = 100
# Write your code here
Display the table of n numbers

Mean of nn inputs

Write a program that calculates the average of the numbers input by the user. The program first asks the user how many values they want to average. The number of values should be greater than 00.

Sample input

4
10
20
30
40

Sample output

The average is : 25
# Write your code here
Display mean of n inputs

Factors of user input

Write a program that shows all the factors of a number provided by the user.

Sample input

20

Sample output

1
2
4
5
10
20
# Write your code here
Display the factors of the user input

Prime or not

A prime number is divisible only by itself and 11.

Write a program that shows whether or not the natural number input by the user is a prime number.

Sample input 1

7

Sample output 1

7 is a prime number

Sample input 2

20

Sample output 2

20 is not a prime number
# Write your code here
Display whether or not a number is prime