Sum of numbers in a range (in Ruby)
I must honestly say that this challenge was my favorite so far. You know why?! Because I have cracked it just by myself within minutes without consulting any additional advice. #ifeelthepower
So here is the task:
# Write a method that takes in an integer `num` and returns the sum of
# all integers between zero and num, up to and including `num`.
My solution is following: I used a for loop with a range including the last integer. Then I increment num by 1 at a time and I return sum when for loop reaches ‘num’.
sum = 0
for num in 0..num do #0...n - if you don't like to include the #last integer
puts sum = num + sum #I print out the values to see how it's #been incremented
num += 1
#output would be