ruby - Using for-loop to add numbers together -
if randomly put in 2 numbers (first number smaller), how use for-loop add numbers between , itself?
ex: first number: 3
second number: 5
the computer should give answer of '12'.
how do using for-loop?
simple loop across range defined:
puts "enter first number: " first = gets.to_i puts "enter second number: " second = gets.to_i total = 0 in (first..second) total += end puts total
note if don't enter valid number, converted 0. assumes second number larger first.
Comments
Post a Comment