Java math FAQ: How do I square a number in Java?You can square a number in Java in at least two different ways:
Multiply the number by itself
Call the Math.powfunction
The following examples demonstrate these approaches.
1) Java: Square a number by multiplying it by itself
This is how you square a number by multiplying it by itself:
int i = 2;
int square = i * i;
In this example if you print the value of square, it will be 4.
2) Java: Square a number with the Math.pow
Multiply the number by itself
Call the Math.powfunction
The following examples demonstrate these approaches.
1) Java: Square a number by multiplying it by itself
This is how you square a number by multiplying it by itself:
int i = 2;
int square = i * i;
In this example if you print the value of square, it will be 4.
2) Java: Square a number with the Math.pow
[
Collection
]
[
|
...
]