Find Square Root of an Integer

Find Square Root of an Integer. How to calculate square root in java without sqrt function.

Given an integer x (non-negative integer). Write a code to compute the square root of x.

If x is not a perfect square in that case return floor of square root of x (floor(sqrt(x))).

NOTE: Do not use the sqrt function from the standard library.

For example –

Example 1:

           Input: 4

Output: 2

Example 2:

          Input: 11

          Output: 3

The square root of 11 is 3.316624 but we have returned the integer part only after truncating the decimal digits.

floor(3.316624) is 3

Example 3:

         Input: 17

         Output: 4

The square root of 17 is 4.12310. Floor(4.12310) is 4.