## How to square a number in Java

In Java, when a number is multiplied by itself it is said to be the square of a number. Java uses two ways through which the square of a number can be found.

## Method 1: Using the Math.pow() method to get the square

Java supports various methods that can be used to get the square of a number. Among these methods, Math.pow() is dedicated to finding the square of a number. This method only returns a double data type value.

**Syntax:**

This syntax shows that the **Math.pow** method takes two arguments. The first argument is a number and the second argument is the power of the number which means the second method specifies the number of times a number can multiply by itself. We can use this method to find the 3rd and 4th power as well but to find the square of a number you need to give 2 as a second argument.

**Code:**

public static void main(String[] args) {

int d = 5;

double f;

f=Math.pow(d, 2);

System.out.println("The square of "+d+" = "+f);

}

}

In this code, we have declared an integer **d** and assigned it a value **5.** After then, the **Math.pow()** method is applied to get the square of the integer “**d**”. The value of the square is stored in a variable “**f**”.

**Output:**

The output clearly shows that the Math.pow() method takes **d** (a variable that contains a value) and the power **2** as an argument whereas 2 represents the number of times d multiplied by itself. Then we finally got the required result.

## Method 2: Multiplying the number by itself

The square of a number is basically the multiplication of the number by itself. Here, the example code describes how a number can be multiplied by itself to get the square:

**Code:**

public static void main(String[] args) {

int d = 5;

System.out.println("The square of "+d+" = "+d*d);

}

}

In this code, we have declared a variable **d** that contains a value **5**. Then we multiply **d** by itself to get the square of **5**.

**Output:**

This output shows that we get the square of a number by multiplying **d** by itself.

Here you go! Several methods are explained to find the square of a number.

## Conclusion

In Java, we can find the square of a number by using the Math.pow() method. However, you can also multiply a number by itself to get the square. To find a square using the Math.pow() method, you need to pass the number and value “**2**” as arguments to the Math.pow() method. In this post, we have demonstrated the working and functionality of these two possible ways to get the square of a number in Java.