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:
Example:
Suppose the given number is 5.
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
- Import Scanner class: The
java.util.Scannerpackage is imported to accept input from the user. - Declare the class: The
Mainclass is created to execute the Java program. - Create Scanner object: The Scanner object is created using
new Scanner(System.in)to read keyboard input. - Accept user input: The
nextDouble()method reads the number entered by the user and stores it in thenumvariable. - Calculate the square: The multiplication operator (*) multiplies the number by itself.
double square = num * num; - Display the result: The
System.out.println()statement displays the calculated square. - 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
- Declare an integer variable
numand initialize it with 8. - Create another variable named
squareto store the result. - Multiply
numby itself usingnum * num. - Store the calculated result in the
squarevariable. - 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
- Import the Scanner class to accept input from the user.
- Create a Scanner object to read the input.
- Read the number using the
nextDouble()method. - Use
Math.pow(num, 2)to calculate the square. - Store the result in the
squarevariable. - 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
| Step | Operation | Value |
|---|---|---|
| 1 | Read input from user | num = 5 |
| 2 | Multiply the number by itself | 5 × 5 |
| 3 | Calculate the result | 25 |
| 4 | Store the result | square = 25 |
| 5 | Display the result | 25.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.
| Complexity | Value | Explanation |
|---|---|---|
| Time Complexity | O(1) | Only one multiplication operation is performed. |
| Space Complexity | O(1) | A fixed number of variables are used. |