Java

How to Get the Square, Cube, and Square Root of a Number in Java

While dealing with mathematical computations in Java, there can be instances where the programmer needs to return the square, cube, and square root of the number. For instance, multiplying a particular number with an identical number multiple times efficiently or computing the square root in accordance with the given requirement. In such cases, fetching the “square”, “cube”, and “square root” of a number in Java is assistive in reducing the code complexity by providing the dedicated methods for such calculations.

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

Math.pow(num1, num2)

In the above syntax:

  • num1” refers to the base.
  • num2” corresponds to the exponent.
public static double sqrt(double a)

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:

public class sqrtcr {
 public static void main(String args[]){
  int value= 5;
  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));
}}

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”:

import java.util.Scanner;

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 class sqrtcr2 {
 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.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.