To find the square root of a number in Ruby, we can use the Math.sqrt()
method which returns the square root of a given number.
Here's an example:
main.rb104 chars4 lines
Output:
main.rb29 chars2 lines
In the above code, we first define the variable number
and set it equal to 25. We then use the Math.sqrt()
method to find the square root of number
and store the result in the variable square_root
. Finally, we print the result using puts
statement.
Note that the Math.sqrt()
method can also be used with variables or expressions, not just with hardcoded numbers.
gistlibby LogSnag