Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Ruby | Integer sqrt() function

  • Last Updated : 07 Jan, 2020

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!