# Square Root without square root. snippet in c

## Square Root without square root.

user4109

``````#include<stdio.h>

void main()
{
int number;

float temp, sqrt;

printf("Provide the number: \n");

scanf("%d", &number);

// store the half of the given number e.g from 256 => 128
sqrt = number / 2;
temp = 0;

// Iterate until sqrt is different of temp, that is updated on the loop
while(sqrt != temp){
// initially 0, is updated with the initial value of 128
// (on second iteration = 65)
// and so on
temp = sqrt;

// Then, replace values (256 / 128 + 128 ) / 2 = 65
// (on second iteration 34.46923076923077)
// and so on
sqrt = ( number/temp + temp) / 2;
}

printf("The square root of '%d' is '%f'", number, sqrt);
}
``````

## Square Root without square root.

user4142

``````# Python3 implementation of the approach
import math

# Recursive function that returns square root
# of a number with precision upto 5 decimal places
def Square(n, i, j):

mid = (i + j) / 2;
mul = mid * mid;

# If mid itself is the square root,
# return mid
if ((mul == n) or (abs(mul - n) < 0.00001)):
return mid;

# If mul is less than n, recur second half
elif (mul < n):
return Square(n, mid, j);

# Else recur first half
else:
return Square(n, i, mid);

# Function to find the square root of n
def findSqrt(n):
i = 1;

found = False;
while (found == False):

# If n is a perfect square
if (i * i == n):
print(i);
found = True;

elif (i * i > n):

# Square root will lie in the
# interval i-1 and i
res = Square(n, i - 1, i);
print ("{0:.5f}".format(res))
found = True
i += 1;

# Driver code
if __name__ == '__main__':
n = 3;

findSqrt(n);

# This code is contributed by 29AjayKumar
``````

## Square Root without square root.

user1529

``````// Java implementation of the approach
import java.util.*;

class GFG
{

// Recursive function that returns
// square root of a number with
// precision upto 5 decimal places
static double Square(double n,
double i, double j)
{
double mid = (i + j) / 2;
double mul = mid * mid;

// If mid itself is the square root,
// return mid
if ((mul == n) ||
(Math.abs(mul - n) < 0.00001))
return mid;

// If mul is less than n,
// recur second half
else if (mul < n)
return Square(n, mid, j);

// Else recur first half
else
return Square(n, i, mid);
}

// Function to find the square root of n
static void findSqrt(double n)
{
double i = 1;

boolean found = false;
while (!found)
{

// If n is a perfect square
if (i * i == n)
{
System.out.println(i);
found = true;
}

else if (i * i > n)
{

// Square root will lie in the
// interval i-1 and i
double res = Square(n, i - 1, i);
System.out.printf("%.5f", res);
found = true;
}
i++;
}
}

// Driver code
public static void main(String[] args)
{
double n = 3;

findSqrt(n);
}
}

``````