Reassigning the value of variables in Ruby?

Try something like this where you use lower case variable names rather than upper case constant names:

def square_root(radicand)
    a, b = radicand, 1
    tolerance = 0.00000000000000000001

    while (a - b).abs > tolerance
        a = (a + b) / 2
        b = radicand / a


print "Enter the radicand:"
radicand = gets.to_f
puts "The square root of #{radicand} is #{square_root(radicand)}"

Example Usage:

Enter the radicand: 256
The square root of 256.0 is 16.0

Browse More Popular Posts

Leave a Comment