0

How to return the sum of a number in ruby?

sum(num) end sum(4) # => 10 because 1+2+3+4 = 10

13th Nov 2019, 8:40 PM
NELSON J
NELSON J - avatar
2 Answers
16th Nov 2019, 7:36 PM
Dmitry Ilin
Dmitry Ilin - avatar
0
def sum(num) i = 0 sum = 0 while i < num i += 1 sum += i end return sum end puts sum(4) puts sum(3)
19th Nov 2019, 1:00 AM
NELSON J
NELSON J - avatar