Open In App

Ruby | Integer sqrt() function

Improve
Improve
Like Article
Like
Save
Share
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:




#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



Last Updated : 07 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads