Q:

Ruby program to demonstrate the nested \'for\' loop

belongs to collection: Ruby Looping Programs

0

In this program, we will print tables from 2 to 5 using a nested "loop" loop.

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 nested "for" loop is given below. The given program is compiled and executed successfully.

# Ruby program to demonstrate the 
# nested "for" loop

cnt1=2;
cnt1=0;

for cnt1 in 2..5 do   
    for cnt2 in 1..10 do   
        print (cnt1*cnt2)," ";
    end
    puts;
end

Output:

2 4 6 8 10 12 14 16 18 20 
3 6 9 12 15 18 21 24 27 30 
4 8 12 16 20 24 28 32 36 40 
5 10 15 20 25 30 35 40 45 50

Explanation:

In the above program, we created two variables cnt1cnt2 that are initialized with 2,0 respectively. Then we used nested for loop to print tables from 2 to 5.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby program to demonstrate the nested \'do\&... >>
<< Ruby program to demonstrate the nested \'whil...