Loops with Ranges
Learn about fixed iteration loops that have a large number of iterations with different intervals.
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:
- The range specified with
..
generates the values, including the upper limit. For example,0..100
generates values to .
- The range specified with
...
generates the values excluding the upper limit. For example,0...100
generates values to .
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 and .
Loop with ..
and ...
Let’s say we want to produce a program that counts from to . One way to do this is with the help of a range, as follows:
for a in 1..100 # Using range in for loopprint(a, "\n")end
The 1..100
sequence produces integers from to . It works exactly the same as listing these values in square brackets separated by commas.
Let’s take another example of generating the first non-negative even numbers: , , .
for a in 0...100 # Using range in for loope = a * 2 # Multiple each value of a with 2print(e, "\n")end
In this program, ...
generates values from to , and we multiply each value by , which displays 0*2
, 1*2
, 2*2
… 99*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:
for a in (0...200).step(2) # Using range function with step parameterprint(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 .
# Write your code here
Arithmetic sequence
Write a program that prints the terms of the sequence .
# Write your code here
Table of
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
Mean of 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 .
Sample input
4
10
20
30
40
Sample output
The average is : 25
# Write your code here
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
Prime or not
A prime number is divisible only by itself and .
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