Register Login

How to Find Square Root in Java

Updated Jan 08, 2020

Find Square Root in Java

To find out the square root of any number in you can use the following methods:

  • Using Math.sqrt() Function
  • Using Custom Function
  • Without using any function

1) Java Math.sqrt() Function

The math.sqrt() function is an in-built function in Java, which returns the square root of a double value argument. 

Notes:

  • If the input argument is NaN (Not a Number) or negative than the return value will be NaN
  • If the input argument is positive infinity, then the return value will also be positive infinity.
  • If the input argument is positive zero, then the return value will be positive zero.
  • If the input argument is negative zero, then the return value will be negative zero.

Syntax

public static double sqrt(double arg)

Parameter

The math.sqrt() function takes only one parameter which is:

  • arg: an argument whose square root has to be taken.

Return Value

This math.sqrt() function returns square root of a double value argument

Java program to find the square root of a number using math.sqrt()

//Java program to find the square root of a number

//Simple Program (With inbuilt function)

//Importing Scanner Class of util package
import java.util.Scanner;
//Importing Math Class of lang package
import java.lang.Math;

//Main Class of the program
public class Main
{
    //Main method of the program
    public static void main(String[] args) {
        //Giving hint to the user for, What to enter
        System.out.print("Please enter the number : ");
        //Creating the object of Scanner Class
        Scanner input = new Scanner(System.in);
        //Taking input from user
        float number_1 = input.nextFloat();
        //Printing the output
        System.out.println("Square root of "+ number_1+ " is: "+Math.sqrt(number_1));
    }
}

OUTPUT:

Please enter the number: 30
Square root of 30.0 is: 5.477225575051661

2) Square Root in Java Using Custom Function

//Java program to find the square root of a number

//Simple Program (Using Custom Function)

//Importing Scanner Class of util package
import java.util.Scanner;

//Main Class of the program
public class Main
{
  //Custom method to find square root
  public static double findSquareRoot(float number){
    double number_2 = number / 2;
    double temp;
    do
        {
          temp = number_2;
          number_2 = (temp + (number / temp)) / 2;
        }while ((temp-number_2) != 0 );

    //Returning the result

    return number_2;

  }
    //Main method of the program
    public static void main(String[] args) {
        //Giving hint to the user for, What to enter
        System.out.print("Please enter the number : ");
        //Creating the object of Scanner Class
        Scanner input = new Scanner(System.in);
        //Taking input from user
        float number_1 = input.nextFloat();
        //Printing the result
        System.out.println("Square root of "+ number_1+ " is: "+findSquareRoot(number_1));
    }
} 

Output

Please enter the number: 30
Square root of 30.0 is: 5.477225575051661

3) Square Root in Java without using Math.sqrt() Function

//Java program to find the square root of a number

//Simple Program (Without inbuilt function)

//Importing Scanner Class of util package
import java.util.Scanner;

//Main Class of the program
public class Main
{
    //Main method of the program
    public static void main(String[] args) {
        //Giving hint to the user for, What to enter
        System.out.print("Please enter the number : ");
        //Creating the object of Scanner Class
        Scanner input = new Scanner(System.in);
        //Taking input from user
        float number_1 = input.nextFloat();
        //Login Start
        double number_2 = number_1 / 2;
        double temp;
        do
        {
          temp = number_2;
          number_2 = (temp + (number_1 / temp)) / 2;
        }while ((temp-number_2) != 0 );
        //Logic Ends
        System.out.println("Square root of "+ number_1+ " is: "+number_2);
    }
}



Output:

Please enter the number: 30
Square root of 30.0 is: 5.477225575051661


×