Ruby | Integer sqrt() function
The sqrt() function in Ruby returns the integer square root of the non-negative integer n, i.e. the largest non-negative integer less than or equal to the square root of n
Syntax: Integer.sqrt(number)
Parameter: The function takes the integer whose square root is to be returned. It throws an error “out of domain” if a negative number is passed.
Return Value: The function returns the integer square root.
Example 1:
#Ruby program for sqrt() function #Initializing the number num1 = 25 num2 = 16 num3 = 100 num4 = 5 #Prints the sqrt of a number puts Integer.sqrt(num1) puts Integer.sqrt(num2) puts Integer.sqrt(num3) puts Integer.sqrt(num4) |
Output:
5 4 10 2
Example 2:
#Ruby program for sqrt() function #Initializing the number num1 = 64 num2 = 81 num3 = 49 num4 = 36 #Prints the sqrt of a number puts Integer.sqrt(num1) puts Integer.sqrt(num2) puts Integer.sqrt(num3) puts Integer.sqrt(num4) |
Output:
8 9 7 6
Reference: https://devdocs.io/ruby~2.5/integer#method-c-sqrt
Recommended Posts:
- Ruby | Math sqrt() function
- Ruby | BigDecimal sqrt() function
- Ruby Integer integer? function with example
- Ruby | Integer << function
- Ruby Integer lcm() function with example
- Ruby Integer abs() function with example
- Ruby Integer odd? function with example
- Ruby Integer div() function with example
- Ruby | Integer - function
- Ruby Integer gcd() function with example
- Ruby Integer even? function with example
- Ruby | Integer >= function
- Ruby | Integer <=> function
- Ruby Integer chr function with example
- Ruby Integer next function with example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


