Thursday, 28 Oct 2021
Programming

How Can You Square a Number in Java?

Java is a very versatile programming language. Therefore there are a number of ways to carry out arithmetic operations in Java. Here we have discussed all the possible methods by which you can square a number in java.

1. How to calculate the square of a number in java using simple variables

Here you don’t require to know extra methods to carry out the square of a number. The procedure here is simple, input a number and find its square by multiplying it twice with itself.

Algorithm for square of a number

  1. Declare the class of the program
  2. Create an object to accept inputs from the user
  3. Create variables to accept input and store the final result
  4. Multiply the input twice and store it in a variable
  5. Print the variable where the final result is stored

Code for square of a number

Output for the above java code

enter the number: 10

The square of 10 is 100

2. Calculating square of a number in Java using method Math.pow()

The first example was for beginners. Now, if you have advanced a little more in Java and already know the use of the Java Math class, then this method is for you. Java has a special class called Math, which contains methods that help you to perform some arithmetic operations. Some examples of such methods are Math.max(x,y),Math.sqrt(x)and Math.pow(x,y). Here, we will use the method Math.pow(x,y).

What is math.pow in Java?

As we already know, Math.pow is a method in Java that is used to calculate the power of a number raised to the power of another number. The method accepts two parameters and returns the first number’s value raised to the second number’s power. However, while using this function, you should keep some special cases in mind.

  • If the second parameter is zero (it doesn’t matter positive or negative), the final value will be 1.0.
  • If the second parameter is 1, then the final value remains the same.
  • The result becomes NaN if the second parameter is NaN.

Algorithm for square of a number using Math.pow()

  1. Declare the class of the program
  2. Create an object to accept inputs from the user
  3. Create variable and accept input and store final result
  4. Calculate the square using Math.pow() function
  5. Store the result in a variable
  6. Print the variable where the final result is stored

Code for square of a number using Math.pow()

Output for the above java code

enter the number: 10

The square of 10 is 100.0

3. How to calculate the square of a number in java using recursion?

Now finally, coming to the last method of squaring a number in java. After you have aced the basics of java, you can use recursion to calculate the square of a number.

What is recursion in java?

Recursion is a process in which a function calls itself. And the corresponding function is known as a recursive function. Recursion can come in very handy while solving some complex problems like Tower of Hanoi. The idea behind recursion is to use smaller solutions to solve a large problem by adding base cases and other conditions.

Example of a recursive function

//here we are calculating the factorial of a number

int fact(int n)

{

    if (n < = 1) // base case

        return 1;

    else   

        return n*fact(n-1);   

}

Algorithm for square of a number using recursion

  1. Declare the class of the program
  2. Create an object to accept inputs from the user
  3. Create variable and accept input and store final result
  4. Accept the input from the user
  5. Write the print statement and call the recursive function in the printing statement itself
  6. Close the main function
  7. Start the recursive function and pass an integer as an argument
  8. Return the square of the integer
  9. Finally, close the recursive function

Output for the above java code

enter the number: 10

The square of 10 is 100

Overview

Finding the square of a number using java is relatively easy. However, the technique you choose will depend on your expertise in the programming language. Don’t just copy the code from the above snippets. Try yourself first and then take help.