Question and Answer

how to square a number in java | Java Tutorial

In this tutorial, you will learn  how to square a number in java. So Let’s get started.

In Java, there are at least two ways to square a number:

  1. Multiply the number by itself
  2. Call the Math.pow function

These approaches are demonstrated in the following instances.

  1. 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;
				
			
Related Articles