How to Compute Square in Java [Practical Examples]


Java Examples

Different methods to compute Square in Java

The square of a number is the product of the number times itself. In other words, a square is the result of multiplying any number by itself. Hence we can say for a number n, the square of a number will be result=n*n.
There are four different ways to compute square of a number in Java.

  • Using multiplication operator *
  • Using Math.pow() function
  • Using BigInteger.pow() function
  • Using Apache Commons Math API

 

Using multiplication operator *

This is the simplest approach to compute square of a number in Java. Here, we will use * operator to compute the square.
Example :In this example we are finding a square of an integer and a float number.

// Program to compute square of a number
public class Main {

    public static void main(String[] args) {
        // Initializing a variable
        int i = 200;
        float f = 100.5f;

        // Computing a square and printing
        System.out.println("The square of a number " + i + " is " + (i * i));
        System.out.println("The square of a number " + f + " is " + (f * f));
    }

}

Output

The square of a number 200 is 40000
The square of a number 100.5 is 10100.25

 

Using Math.pow() function

In this approach, we will use pow() function of Math class of java.lang package to compute square of a number in Java. The pow() function returns the value of the first argument raised to the power of the second argument. This function accepts two double type parameter and returns a double value as a result. So, the problem with this approach is we cannot use it for integer or a float values.

Example :In this example we are finding a square of a double number using pow() function of Math class.

// Program to compute square of a number
public class Main {

    public static void main(String[] args) {
        // Initializing a variable
        double d = 150.5;

        // Computing a square and printing
        System.out.println("The square of a number " + d + " is " + Math.pow(d, 2.0));
    }

}

Output

The square of a number 100.5 is 10100.25

 

Using pow() of BigInteger Class

In this approach, we will use pow() function of BigInteger class of java.math package to compute square of a number in Java. The disadvantage of the Math.pow() function is they operate only on double values. However, if we want to compute square of a integer or long variable, we have to use pow() function of BigInteger Class. This function takes one parameter as an exponent.

Example :In this example we are finding a square of a integer and long variable. Her, we are using String.valueOf function to convert int and long variable to string type.

// Program to compute square of a number
import java.math.BigInteger;
public class Main {

    public static void main(String[] args) {
        // Initializing a variable
        int i = 110;
        long l = 2250;
        BigInteger b1 = new BigInteger(String.valueOf(i));
        BigInteger b2 = new BigInteger(String.valueOf(l));

        // Computing a square and printing
        System.out.println("The square of a number " + i + " is " + b1.pow(2));
        System.out.println("The square of a number " + l + " is " + b2.pow(2));

    }

}

Output

The square of a number 110 is 12100
The square of a number 2250 is 5062500

 

Using Apache Commons Math API

In this approach, we will need to download commons-math3-3.6.1-bin.zip from the url mentioned in the reference section. Thereafter, we have to add this jar to our existing project folder using the below given steps.

  1. Right click your project folder in eclipse
  2. Click on Build Path -> Add external archives
  3. Select the jar (commons-math3-3.6.1)from the extracted zip folder.
  4. You will see that jar added to Referenced Libraries

Example :In this example we are using ArithmeticUtils.pow() function that can accept long, int and BigInteger type of parameters.

// Program to compute square of a number
import org.apache.commons.math3.util.ArithmeticUtils;

public class temp {
    public static void main(String[] args) {
        long d = 1152;
        System.out.println("The square of a number " + d + " is " + ArithmeticUtils.pow(d, 2));
    }
}

Output

The square of a number 1152 is 1327104

 

Examples to Compute Square in Java

Example 1 : Computing Area of Circle

// Program to compute Area of Circle 
import java.math.BigInteger;
public class Main {

    public static void main(String[] args) {
        // Initializing a variable
        int r1 = 20;
        double r2 = 25.25;

        // Converting int to BigInteger 
        BigInteger b1 = new BigInteger(String.valueOf(r1));

        // Computing area of circle and printing
        b1 = b1.pow(2);
        long v = b1.longValue();
        double res1 = 3.14159 * v;
        double res2 = 3.14159 * (Math.pow(r2, 2));

        System.out.println("The Area of circle with radius " + r1 + " is " + res1);
        System.out.println("The Area of circle with radius " + r2 + " is " + res2);
    }
}

Output

The Area of circle with radius 20 is 1256.636
The Area of circle with radius 25.25 is 2002.959974375

 

Example 2 : Computing Area of Square

// Program to compute Area of Perfect Square 
import java.math.BigInteger;
public class Main {

    public static void main(String[] args) {
        // Initializing a variable
        int side1 = 20;
        double side2 = 25.25;

        // Converting int to BigInteger and computing square
        BigInteger b1 = new BigInteger(String.valueOf(side1));
        b1 = b1.pow(2);

        // Computing square
        double res2 = Math.pow(side2, 2);

        // Printing
        System.out.println("The Area of square with side " + side1 + " is " + b1);
        System.out.println("The Area of square with side " + side2 + " is " + res2);
    }

}

Output

The Area of square with side 20 is 400
The Area of square with side 25.25 is 637.5625

 

Example 3 : Computing hypotenuse using Pythagoras Theorem

The Pythagoras theorem equation is expressed as, c^2 = a^2 + b^2, where 'c' = hypotenuse of the right triangle and 'a' and 'b' are the other two legs.

// Program to compute hypotenuse 
public class Main {

    public static void main(String[] args) {
        double ab = 4;
        double ac = 3;
        double bc;

        // Computing result by applying Pythagoras theorem

        bc = Math.sqrt(Math.pow(ab, 2) + Math.pow(ac, 2));
        System.out.println("The result of computation is " + bc);
    }
}

Output

The result of computation is 5.0

 

Example 4 : Computing result of Series of square

Here, we are computing 10^2 +11^2 + 12^2 + 13^2 + 14^2

// Program to compute series of square of numbers
public class Main {

    public static void main(String[] args) {
        double x = 10;
        double sum = 0;
        double r;

        // Computing result of series
        for (int i = 0; i & lt; 5; i++) {
            // Computing square of a number
            r = Math.pow(x, 2);
            // Computing sum of squares
            sum = sum + r;
            System.out.println(r + " + ");
            x++;
        }

        System.out.println("The result of computation is " + sum);
    }

}

Output

100.0 + 
121.0 + 
144.0 + 
169.0 + 
196.0 + 
The result of computation is 730.0

 

Summary

The knowledge of computing square of a number in Java is very useful while working on a real time applications. In this tutorial, we covered four different approaches to compute square in Java. As per the requirement of an application, we can choose an appropriate approach for computation. We learned in detail about this approaches with an example. All in all, this tutorial, covers everything that you need to know in order to have a clear view on computing a square of a number in Java.

 

References

Math Class
BigInteger Class
Download Apache Commons Math
ArithmeticUtils Class

 

Deepak Prasad

Deepak Prasad

Deepak Prasad is the founder of GoLinuxCloud, bringing over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, Networking, and Security. His extensive experience spans development, DevOps, networking, and security, ensuring robust and efficient solutions for diverse projects.

Certifications and Credentials:

  • Certified Kubernetes Application Developer (CKAD)
  • Go Developer Certification
  • Linux Foundation Certified System Administrator (LFCS)
  • Certified Ethical Hacker (CEH)
  • Python Institute PCAP (Certified Associate in Python Programming)
You can connect with him on his LinkedIn profile and join his Facebook and LinkedIn page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment