 # How to Calculate Square and Square Root in Java?

Last updated on Nov 28,2019 3.9K Views 9 / 29 Blog from Java Programs

One of the most popular frequently asked Java Interview Question is, “Given an integer x, write a java program to find the square root of it”. There are many ways to solve this problem. In this article, let’s check out different ways to find square and square root in Java.

Before discussing the square root code in Java, let’s understand the term square root first.

## Square and Square Root

The square of a number is that number times itself. In other terms, when we multiply an integer by itself we call the product the square of the number. Mathematically,  the square of a number is given as,

Square of n = n*n

For example, the square of number 4 is 4*4 = 16

The square root is just the opposite of the square. The square root of a number, n, is the number that gives n when multiplied by itself. Mathematically, the square root of a number is given as,

Square Root of n = √n

Now that you know what square and square root of a number are, let’s see different ways to calculate them in Java.

## How to Square a Number in Java

You can square a number in Java in two different ways:

1. Multiply the number by itself
2. Call the Math.pow function

### Method1: Square a number by multiplying it by itself

Here’s a Java Program to square a number by multiplying it by itself.

```package MyPackage;
import java.util.Scanner;

public class Square1 {
public static void main(String args[]) {

Double num;
Scanner sc= new Scanner(System.in);

System.out.print("Enter a number: ");
num=sc.nextDouble();

Double square = num*num;
System.out.println("Square of "+ num + " is: "+ square);
}

}
```

Output

```Enter a number: 10
Square of 10.0 is: 100.0
```

### Method2: Square a number with the Math.pow method

Here’s a Java Program to call the Math.pow method to square a number.

```package MyPackage;
import java.util.Scanner;
import java.lang.Math;

public class Square2 {

public static void main(String args[]) {

Double num;
Scanner sc= new Scanner(System.in);

System.out.print("Enter a number: ");
num = sc.nextDouble();

Double square = Math.pow(num, 2);
System.out.println("Square of "+ num + " is: "+ square);
}
}
```

Output

```Enter a number: 22
Square of 22.0 is: 484.0
```

Now let’s check out how to calculate the square root of a number in Java.

## How to Find Square Root of a Number in Java

There are multiple ways to find square root a given number in Java. Let’s explore a few of those.

### Method1: Java Program to Find the square root of a Number using java.lang.Math.sqrt() method

Syntax

public static double sqrt(double x)

• Parameter: x is the value whose square root is to be returned.
• Return: This method returns the square root value of the argument passed to it.
• If parameter x is positive double value, this method will return the square root of x
• When x is NaN or less than zero, this method will return NaN
• If parameter x is positive infinity, this method will return positive Infinity
• When x is positive or negative zero, this method will return the result as Zero with the same sign

Code

```package MyPackage;

public class SquareRoot2 {

public static void main(String args[])
{
double a = 100;

System.out.println(Math.sqrt(a));
// Input positive value, Output square root of x

double b = -81.00;

System.out.println(Math.sqrt(b));
// Input negative value, Output NaN

double c = 0.0/0;
// Input NaN, Output NaN

System.out.println(Math.sqrt(c));

double d = 1.0/0;
// Input positive infinity, Output positive infinity

System.out.println(Math.sqrt(d));

double e = 0.0;
// Input positive Zero, Output positive zero

System.out.println(Math.sqrt(e));
}

}```

Output

```10.0
NaN
NaN
Infinity
0.0
```

### Method2: Java Program to Find the square root of a Number using java.lang.Math.pow() method

We can use the logic √number = number½ to find the square root of a number.

Code

```package MyPackage;

import java.util.Scanner;

public class SquareRoot1 {

public static void main(String[] args)
{
Double num;
Scanner sc= new Scanner(System.in);

System.out.print("Enter a number: ");
num = sc.nextDouble();

Double squareroot = Math.pow(num, 0.5);
System.out.println("The Square of a Given Number  " + num + "  =  " + squareroot);
}
}
```

Output

```Enter a number: 81
The Square of a Given Number  81.0  =  9.0
```

### Method3: Java Program to Find the square root of a Number without using any in-built method

Here’s the logic that we are using: The first sqrt number should be the input number / 2. Here’s a Java Program implementing the above logic.

Code

```package MyPackage;

public class SquareRoot
{

public static double square(double number){
double t;

double squareroot = number / 2;

do {
t = squareroot;
squareroot = (t + (number / t)) / 2;
} while ((t - squareroot) != 0);

return squareroot;
}

public static void main(String[] args)
{
double number = 16;
double root;
root = square(number);
System.out.println("Number : "+number);
System.out.println("Square Root : "+root);
}
}```

Output

```Number : 121.0
Square Root : 11.0
``` REGISTER FOR FREE WEBINAR Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month