Home > Mobile >  Write a program that adds together all the integers from `1` to `250` (inclusive) and `puts`es the t
Write a program that adds together all the integers from `1` to `250` (inclusive) and `puts`es the t

Time:09-30

How would I do this using ruby and only using a while loop with if, elsif and else only?

CodePudding user response:

sum = 0
i = 1

while i < 251
   sum  = i
   i  = 1
end

puts sum

CodePudding user response:

another way to implement this:

n = 250
while true
    puts ( n * (n   1)) / 2
    break
end

Or make the loop entirely a token thing

    n = 250
    while false
    end
    puts ( n * (n   1)) / 2

    
  •  Tags:  
  • ruby
  • Related