Ruby CMath sqrt() Method with example
Last Updated :
07 Jan, 2020
sqrt method is a Math class method in Ruby which is used to calculate the square root of the given value.
Syntax: Math.sqrt(z)
Parameter: Here, z is the value whose square root is to be calculated.
Returns: This method returns the square root of the given number.
Below programs illustrate the use of above-discussed method:
Example 1:
require 'cmath'
a = 9
b = 78 . 15
puts "Square root value of a : #{CMath.sqrt(a)}\n\n"
puts "Square root value of b : #{CMath.sqrt(b)}\n\n"
|
Output:
Square root value of a : 3.0
Square root value of b : 8.84024886527523
Example 2:
require 'cmath'
a = 7841
b = 1234
puts "Sqrt root value of a : #{CMath.sqrt(a)}\n\n"
puts "Sqrt root value of b : #{CMath.sqrt(b)}\n\n"
|
Output:
Sqrt root value of a : 88.54942122905152
Sqrt root value of b : 35.12833614050059
Share your thoughts in the comments
Please Login to comment...