Help with a loop

Hey I’m a noob at ruby so far but learning fast

Basically how could I create a loop

Instead of doing this :

op1 = ((num1%100)/10)
result1 = op1*num2
puts “#{result1}”

op2 = ((num1%1000)/100)
result2 = op2*num2
puts “#{result2}”

op3 = ((num1%10000)/1000)
result3 = op3*num2
puts “#{result3}”

op4 = ((num1%1000000)/100000)
result4 = op4*num2
puts “#{result4}”

op5 = ((num1%10000000)/1000000)
result5 = op5*num2
puts “#{result5}”

op6 = ((num1%100000000)/10000000)
result6 = op6*num2
puts “#{result6}”

op7 = ((num1%10000000000)/100000000)
result7 = op7*num2
puts “#{result7}”

op8 = ((num1%100000000000)/1000000000)
result8 = op8*num2
puts “#{result8}”

op9 = ((num1%1000000000000)/10000000000)
result9 = op9*num2
puts “#{result9}”

So that every time it puts in the zero like the example, but also
outputs the result. for the number of times the loop will happen i will
be using

nummultil = (num1 * num2).to_s.length
num1l = num1.to_s.length

Here is an idea trying not to stray too much from your code.
Modify it the way you want.

(1…9).each do |x|
p num2 * (num1%(10**(x+1))/(10**x))
end

Harry

Fix the ‘max=10’ to fit what you need as maximun iterations count.

def lukas_loop(num1, num2)
max = 10 # I didn’t understand how you calculated the max
iteractions counting.
1.upto(max) do |n|
op = ((num1 % (10**(n+1))) / 10**n )
result = op*num2
puts “#{result}”
end
end

Is it something like this?

I worked it out I think

print "enter the first number: "
num1 = gets.chomp.to_i

print "enter the second number: "
num2 = gets.chomp.to_i

nummultil = (num1 * num2).to_s.length
num1l = num1.to_s.length

num1l.times do |n|
op = ((num1 % (10**(n+1))) / 10**n )
result = op*num2
puts “#{result}”
end

just like this it work

Thanks a lot

-lukas

Abinoam Jr. wrote in post #1125746:

Fix the ‘max=10’ to fit what you need as maximun iterations count.

def lukas_loop(num1, num2)
max = 10 # I didn’t understand how you calculated the max
iteractions counting.
1.upto(max) do |n|
op = ((num1 % (10**(n+1))) / 10**n )
result = op*num2
puts “#{result}”
end
end

Is it something like this?

Ok you guys get the idea, but i need it to output every single time it
does the loop will the example you gave do that ?

Also i will be calculating the amount it loops by the .length of “num1”
so that it cycles through each of the digits and then multiplies each of
the digits by num2

Thanks a lot
-lukas

Edit: I tried your method but it doesn’t seem to work, it doesnt output
anything