How to Square a Number in Java?

Posted in /  

How to Square a Number in Java?
ramyashankar

Ramya Shankar
Last updated on November 23, 2024

    There are many ways to square a number in Java, the simplest of which is multiplying the number by itself. There are also utility methods to do the same. If your project needs to do this often, you can build a function and call the utility as well. We will also see how this utility method can do square, cube, and more operations on a number.

    How to Square a Number in Java?

    The first and simplest method is to multiply the number by itself, as shown below:

    int number = 2;
    int square = number*number;
    System.out.println(square);

    Simple and sweet, isn’t it? Just for the sake of fun, let us take the input from a user:

    int number = new Scanner(System.in).nextInt();

    We have used Scanner to get the input from the user. Users can put any values like 2 and 3. Users can also enter 2.3 or any other decimal number, but in this case, the program will throw an exception! This is what makes this program a bit more complex. We should put a try/catch to handle:

    Exception in thread "main" java.util.InputMismatchException

    And put a message to the user to enter only integer values:

    System.out.println("Enter an integer to get its square:");
    try{
    int number = new Scanner(System.in).nextInt();
    int square = number*number;
    System.out.println(square);
    }
    catch(InputMismatchException time)
    {
    System.out.println("Your entered value doesn't seem to be an integer!");
    }

    Remember to import the following code for the program to run as expected:

    import java.util.InputMismatchException;
    import java.util.Scanner;

    We can also create a separate function to square a number in Java, which can then be called by any class. A way of doing so is:

    public static int calcSquare(int number)
    {
    return number*number;
    }

    Call this method as:

    int square = calcSquare(number);

    The Other Way to Square a Number in Java

    The second way to square a number in Java is to use the math utility function pow. Math.pow() works only with double, because that covers integers too; however, if we have to use the int data type, we have to typecast the return value. For example:

    int number2 = new Scanner(System.in).nextInt();
    int square2 = (int) Math.pow(number2, 2);
    System.out.println(square2);

    We have to put the same try/catch block here as well. Note that this function can be generalized to get any power by changing the second parameter of the function:

    int square2 = (int) Math.pow(number2, 5);
    So, if our number2 is 2, the output will be 2 raised to the power 5 = 32.

    Conclusion

    There are two main points to note here. The first is always handle the required exceptions so that the program exits gracefully. Second, it is preferable to take the input as a double or long instead of int to square a number in Java so that we can apply the logic for any number that the user enters.

    Try the above programs by changing the data type to double. Let us know about your results and understanding of Java square.

    People are also reading:

    Leave a Comment on this Post

    0 Comments