Multiply the number by itselfCall the Math.pow function

The adhering to examples show these philosophies.

You are watching: How to square a number in java

## 1) Square a number by multiplying it by itself

This is exactly how you square a number by multiplying it by itself:

int i = 2;int square = i * i;In this instance if you print the value of square, it will be 4.

## 2) Square a number via the Math.pow method

Here’s how you contact the Math.pow technique to square a number:

int i = 2;int square = Math.pow(i, 2);

## More power multipliers

In general I just multiply the number by itself to get the squared worth, yet the advantage of the Math.pow strategy is that when you understand exactly how to use it, you have the right to quickly cube a number prefer this:

int i = 2;int square = Math.pow(i, 3);Thus, it’s valuable to understand both viewpoints.

See more: How To Insert Micro Sd Card In Samsung Galaxy Tab A, Sd Card: Samsung Galaxy Tab A

In summary, if you wanted to see a couple of ways to square a number (int, float, double, and so on.) in Java, I hope this is valuable.

java
function
java
math
method
power
square
cube
pow

## publications i’ve written    