Home » Java Program » Java Program to Calculate Square of number

Java Program to Calculate Square of number

Last updated:

Support this post with a reaction:

5,688 views

A Java program to find the square of a number calculates the result by multiplying a given number by itself. The square of a number is represented mathematically as (n^2 = n times n). In Java, we can calculate the square using the multiplication (*) operator. We can also use Java 8 Stream API to perform the same operation. In this tutorial, you will learn how to write a Java program to calculate the square of a number using user input, understand the code step by step, and see examples with outputs.

What is Square of Number?

The square of a number is the result obtained by multiplying the number by itself once.

Formula:

Square of a number=n×n=n2 \text{Square of a number} = n \times n = n^2

Example:

Suppose the given number is 5.

52=5×5=25 5^2 = 5 \times 5 = 25

Therefore, the square of 5 is 25.

In Java, we can calculate the square using the following statement:

square = num * num;

Java Program to Find the Square of a Number Using User Input

We can use the Scanner class in Java to accept a number from the user. The multiplication operator is then used to multiply the number by itself and calculate its square.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        double num = sc.nextDouble();

        double square = num * num;

        System.out.println("Square of " + num + " is " + square);

        sc.close();
    }
}

Output

Enter a number: 5
Square of 5.0 is 25.0

Explanation of the Program

  1. Import Scanner class: The java.util.Scanner package is imported to accept input from the user.
  2. Declare the class: The Main class is created to execute the Java program.
  3. Create Scanner object: The Scanner object is created using new Scanner(System.in) to read keyboard input.
  4. Accept user input: The nextDouble() method reads the number entered by the user and stores it in the num variable.
  5. Calculate the square: The multiplication operator (*) multiplies the number by itself.double square = num * num;
  6. Display the result: The System.out.println() statement displays the calculated square.
  7. Close Scanner: The close() method closes the Scanner object after completing the input operation.

Java Program to Find the Square of a Number Without Using Scanner

We can also calculate the square of a number without accepting user input. In this approach, the number is directly initialized in the program.

public class Main {
    public static void main(String[] args) {

        int num = 8;

        int square = num * num;

        System.out.println("Square of " + num + " is " + square);
    }
}

Output

Square of 8 is 64

Explanation of the Program

  1. Declare an integer variable num and initialize it with 8.
  2. Create another variable named square to store the result.
  3. Multiply num by itself using num * num.
  4. Store the calculated result in the square variable.
  5. Print the square using System.out.println().

Java Program to Find the Square of a Number Using Math.pow()

Java provides the built-in Math.pow() method to calculate the power of a number.

The Math.pow() method accepts two arguments:

  • First argument: The base number.
  • Second argument: The exponent or power.

To calculate the square, we pass 2 as the exponent.

Syntax:

Math.pow(number, 2);
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        double num = sc.nextDouble();

        double square = Math.pow(num, 2);

        System.out.println("Square of " + num + " is " + square);

        sc.close();
    }
}

Output

Enter a number: 6
Square of 6.0 is 36.0

Explanation of the Program

  1. Import the Scanner class to accept input from the user.
  2. Create a Scanner object to read the input.
  3. Read the number using the nextDouble() method.
  4. Use Math.pow(num, 2) to calculate the square.
  5. Store the result in the square variable.
  6. Display the calculated result.

Note: The Math.pow() method returns a double value. For a simple square calculation, the multiplication operator is generally the preferred approach.

Dry Run of the Java Square Program

Let’s understand how the program executes when the user enters 5 as input.

Input:

num = 5
StepOperationValue
1Read input from usernum = 5
2Multiply the number by itself5 × 5
3Calculate the result25
4Store the resultsquare = 25
5Display the result25.0

Final Output:

Square of 5.0 is 25.0

Therefore, the square of 5 is 25.

Time and Space Complexity of the Java Square Program

The program calculates the square using a single multiplication operation. Therefore, it requires constant time and constant auxiliary space.

ComplexityValueExplanation
Time ComplexityO(1)Only one multiplication operation is performed.
Space ComplexityO(1)A fixed number of variables are used.