How to Square a Number in Java

We can square a number in Java in no less than two different ways. Let have a look on each method one by one.

Method1: Multiplying the Number by Itself

To square a number x, we can multiply the number by itself.

Y = x * x.

Java Program for the above method.


Enter a number: 5
The Square of the number is : 25

Method 2: Using the Math.pow() Function

Lets have a look how we can find square of number in java using Math.pow() function.


Enter a number: 7
The Square of the number is : 49.0

Here, the advantage of using Math.pow() function is that using it we can extend the program to find any power of the number.

Math.pow(num, 2) for square, Math.pow(num, 3) for cube, Math.pow(num, 8) for num to the power of 8.

Leave a Comment

Your email address will not be published. Required fields are marked *