find the square root of a number in ruby

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.rb
number = 25  
square_root = Math.sqrt(number)  
puts "The square root of #{number} is #{square_root}"  
104 chars
4 lines

Output:

main.rb
The square root of 25 is 5.0
29 chars
2 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