Java Program to Calculate Square of number

In this tutorial, we will learn writing Java program to calculate the square of a given number. Our program will take a number as an input and will return the square of that number as output.

What is Square of Number?

When a number is multiplied to itself once, resultant number is known as square of number.

For example:

Suppose we want to calculate the square of 5.

Square of 5 = 5^2 = 5*5 = 25.

Program1: Calculate square of number in Java

In this program we have applied straightforward approach. Here we are taking the input number from the user and calculating the square using a simple mathematical expression (num * num). After calculating the result, we are printing it on the console. The output for the input 5 was correctly computed and printed as 25.0.

import java.util.*;
public class Main
{
    public static void main(String[] args) {
        float num,square;
        Scanner sc = new Scanner(System.in);
        System.out.println("Please give the number to calculate square");
        num= sc.nextFloat();
        square = num*num;
        System.out.println("Square of "+num+" is ");
        System.out.printf("%.2f", square);
    }
}

Output

Please give the number to calculate square
5
Square of 5.0 is 
25.00

Explanation

For input 5, the output generated by our program is 5.0*%.0 i.e. equals 25.0.

Program 2: Calculate Square using Java 8

In this program we are using Java 8 streams to calculate the square of a number. The DoubleStream.of(num).map(x -> x * x) expression is used here to find square of a given input number. And the result is printed using the findFirst operation. The output for the input 6 is correctly computed and printed as 36.0.

import java.util.*;
import java.util.stream.*;
public class Main
{
    public static void main(String[] args) {
        float num;
        double square;
        Scanner sc = new Scanner(System.in);
        System.out.println("Please give the number to calculate square");
        num= sc.nextFloat();
        square = DoubleStream.of(num)
                .map(x -> x * x)
                .findFirst()
                .orElse(Double.NaN);
        System.out.println("Square of "+num+" is ");
        System.out.printf("%.2f", square);
    }
}

Output

Please give the number to calculate square
6
Square of 6.0 is 
36.00

Conclusion

In this tutorial you have learnt writing Java programs to calculate the square of a given number. The square of a number is calculated by multiplying the number by itself once.

Program 1: Calculate Square of a Number in Java

In the first program we have used straightforward logic to calculate the square of number. We have multiplied input number to itself to calculate the square of a number,. At the last we have printed the output. The mathematical expression (num * num) is used to calculate the square. The result was then printed to the console. The output for the input 5 was correctly computed as 25.0.

Program 2: Calculate Square using Java 8

In the second program we have used the Java 8 streams. The DoubleStream.of(num).map(x -> x * x) expression is applied to find the square of the input number. The result was obtained using the findFirst operation. For the input 6, output was correctly computed as 36.0.

Hope above tutorial helped you to understand writing Java program to calculate the power of a given input number.

Happy coding!