how to calculate the sum of 1+2+3+.....+n using ruby? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 1

how to calculate the sum of 1+2+3+.....+n using ruby?

5th Nov 2017, 7:07 AM
rakibul
4 Respuestas
+ 18
puts (1..gets.to_i).inject(:+)
5th Nov 2017, 8:21 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 5
is Rudy easy
10th Dec 2017, 10:25 AM
Vyshnav Vishnu
Vyshnav Vishnu - avatar
+ 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}"
5th Nov 2017, 8:03 AM
Paul
Paul - avatar
+ 2
# In python sum = 0 for i in range(1, n+1): sum += i print(sum)
7th Nov 2017, 3:50 PM
#RahulVerma
#RahulVerma - avatar