mercredi 6 mai 2015

Get sum of numbers within range (from 0 to user input)

This is the code i am using, its purpose is for the user to enter an integer, the program will then take the sum of all the numbers up to and including the one entered. There is probable an easier way to do this

 sum = 0
 puts "please enter a number"
 counter = gets.chomp.to_i
 begin
  sum += counter
  counter -= 1
 end while counter == 0

Aucun commentaire:

Enregistrer un commentaire