Java

# How to square and square-root a number in Java

2 x 2 = 4

is written as,

22 = 4

and the square of 2 is 4, while the square root of 4 is 2. The big 2 is the base, and the other 2 is the index.

3 x 3 = 9

is written as,

32 = 9

and the square of 3 is 9, while the square root of 9 is 3. 3 is the base and 2 is the index.

4 x 4 = 16

is written as,

42 = 16

and the square of 4 is 16, while the square root of 16 is 4. 4 is the base and 2 is the index.

5 x 5 = 25

is written as,

52 = 25

and the square of 5 is 25, while the square root of 25 is 5. 5 is the base and 2 is the index.

When a number is multiplied by itself, the result is the square of the number. That is, if an integer is multiplied by itself, the result of the multiplication is the square integer. The reverse of the square integer is the square root. If a double type number is multiplied by itself, the result is the square of the double type number. The reverse of the square double type number is the square root. Note: the square root of an ineger can still be a double type number.

The Java Math class has the pow() method to find squares and the sqrt() method to find square roots. The Math class is in the java.lang.* package. When a class to be used is in the java.lang.* package, this package does not have to be imported.

## Squaring a number in Java

### public static double pow(double a, double b)

This sub-heading is the syntax for the pow method of the Math class. “pow” stands for “power”, which means a base raised to an index. The method is static, meaning that the Math object does not have to be instantiated for the method to be used. In this case, the name of the class, “Math” is used followed by the dot and then the method name. The method is public, meaning that it can be accessed from outside the class code.

The first argument of this method is the base, while the second argument is the index. Both arguments are of the double type. The method returns a double, which is the power of the double type. The power is the base raised to an index. In the case of square, the index must be 2 and nothing else.

The following program, outputs the square of 2:

public class TheClass {
public static void main(String[] args) {
double val = 2;
double pw = Math.pow(val, 2);
System.out.println(pw);
}
}

The output is 4.0. For the same output, the code could simply have been written as:

public class TheClass {
public static void main(String[] args) {
System.out.println(Math.pow(2, 2));
}
}

The following program, outputs the square of 3:

public class TheClass {
public static void main(String[] args) {
double val = 3;
double pw = Math.pow(val, 2);
System.out.println(pw);
}
}

The output is 9.0. For the same output, the code could simply have been written as:

public class TheClass {
public static void main(String[] args) {
System.out.println(Math.pow(3, 2));
}
}

The following program, outputs the square of 4:

public class TheClass {
public static void main(String[] args) {
double val = 4;
double pw = Math.pow(val, 2);
System.out.println(pw);
}
}

The output is 16.0. For the same output, the code could simply have been written as:

public class TheClass {
public static void main(String[] args) {
System.out.println(Math.pow(4, 2));
}
}

The following program, outputs the square of the double type number, 2.5:

public class TheClass {
public static void main(String[] args) {
double val = 2.5;
double pw = Math.pow(val, 2);
System.out.println(pw);
}
}

The output is 5.25. For the same output, the code could simply have been written as:

public class TheClass {
public static void main(String[] args) {
System.out.println(Math.pow(2.5, 2));
}
}

## Square-root of a number in Java

### public static double sqrt(double a)

This sub-heading is the syntax for the square-root method of the Math class. “sqrt” stands for “square root”, which means the number that will be multiplied by itself to give the result (number in question). The method is static, meaning that the Math object does not have to be instantiated for the method to be used. In this case, the name of the class, “Math” is used followed by the dot and then the method name. The method is public, meaning that it can be accessed from outside the class code.

There is only one argument to this method: the square result (the number whose square root is needed). The argument is of the double type. The method returns a double, which is the double square root of the double typed argument. The square-root is the base that was raised to the index, 2.

The following program, outputs the square-root of 4:

public class TheClass {
public static void main(String[] args) {
double val = 4;
double rt = Math.sqrt(val);
System.out.println(rt);
}
}

The output is 2.0. For the same output, the code could simply have been written as:

public class TheClass {
public static void main(String[] args) {
System.out.println(Math.sqrt(4));
}
}

The following program, outputs the square-root of 9:

public class TheClass {
public static void main(String[] args) {
double val = 9;
double rt = Math.sqrt(val);
System.out.println(rt);
}
}

The output is 3.0. For the same output, the code could simply have been written as:

public class TheClass {
public static void main(String[] args) {
System.out.println(Math.sqrt(9));
}
}

The following program, outputs the square-root of 16:

public class TheClass {
public static void main(String[] args) {
double val = 16;
double rt = Math.sqrt(val);
System.out.println(rt);
}
}

The output is 4.0. For the same output, the code could simply have been written as:

public class TheClass {
public static void main(String[] args) {
System.out.println(Math.sqrt(16));
}
}

The following program, outputs the square-root of the double type number, 6.25:

public class TheClass {
public static void main(String[] args) {
double val = 6.25;
double rt = Math.sqrt(val);
System.out.println(rt);
}
}

The output is 2.5. For the same output, the code could simply have been written as:

public class TheClass {
public static void main(String[] args) {
System.out.println(Math.sqrt(6.25));
}
}

## Conclusion

If a number is multiplied by itself, the result is the square of the number. The reverse is the square root. The Java Math method syntax for the square of a number is:

public static double pow(double a, double b)

where the second argument is always 2 and the first argument is the number whose square is needed.

The Java Math method syntax for the square-root of a number is:

public static double sqrt(double a)

where the number of interest is the only argument. 