This blog will elaborate on the approaches to fetch the “square”, “cube”, and “square root” of a number in Java.
How to Get the Square, Cube, and Square Root of a Number in Java?
The square and cube of the number can be computed using the “Math.pow()” method. This method is used to evaluate a number based on the specified power. It works in such a way that the first parameter is evaluated based on the power specified as its second parameter.
The square root of a number, however, can be calculated via the “Math.sqrt()” method. This method returns the rounded positive square root of the corresponding number.
Syntax
In the above syntax:
- “num1” refers to the base.
- “num2” corresponds to the exponent.
In this syntax, “a” indicates the value whose square root needs to be computed.
Example 1: Get the Square, Cube, and Square Root of the “Specified” Number in Java
In this example, the square, cube, and square root of the specified number can be computed:
In the above code snippet, apply the following steps:
- Firstly, initialize the provided integer value.
- After that, apply the “pow()” method to calculate the square, and cube of the initialized integer by passing it as the method’s argument.
- This can be done by specifying the corresponding exponent, i.e., “2” and “3”, respectively followed by the passed integer.
- Lastly, compute the square root of the initialized integer via the “sqrt()” method.
Output
In this output, it can be observed that the square, cube, and square root of the provided integer have been computed appropriately.
Before proceeding to the next example, include the below-provided package to enable “user input”:
Example 2: Get the Square, Cube, and Square Root of the “User-defined” Number in Java
This example computes the square, cube, and square root of the user input integer:
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Enter the number: ");
int value = input.nextInt();
System.out.println("The square of the number is: " + Math.pow(value, 2));
System.out.println("The cube of the number is: " + Math.pow(value, 3));
System.out.println("The square root of the number is: " + Math.sqrt(value));
}}
According to the above lines of code, perform the following steps:
- First of all, create a “Scanner” object using the “new” keyword and the “Scanner()” constructor, respectively.
- The “in” parameter takes the user input.
- The associated “nextInt()” method ensures the user input value as an “Integer”.
- Now, likewise apply the “pow()” method to return the square and cube of the user input integer, respectively.
- Finally, apply the “sqrt()” method similarly to calculate the square root of the user input integer.
Output
The above outcome implies that the user input integer is evaluated for square, cube, and square root accordingly.
Conclusion
In Java, the square, and cube of the number can be computed using the “Math.pow()” method. The square root of a number, however, can be calculated via the “Math.sqrt()” method. The square and cube can be computed by specifying their corresponding exponents and the latter method is dedicated to computing the square root of the number. This blog demonstrated the methods to return the square, cube, and square root of the specified or user input number in Java.