Open In App

Ruby | Integer sqrt() function

Last Updated : 07 Jan, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
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: CPP
#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: CPP
#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

Next Article

Similar Reads