+ 1
how to calculate the sum of 1+2+3+.....+n using ruby?
5 Réponses
+ 18
puts (1..gets.to_i).inject(:+)
+ 5
is Rudy easy
+ 2
I think this will do it:
#program to add numbers
puts 'Enter a numbr'
x = gets.chomp.to_i
y = 1
z = 0
while y <= x do
z += y
y += 1
end
puts "This is the result #{z}"
+ 2
# In python
sum = 0
for i in range(1, n+1):
sum += i
print(sum)