Java Program to Find Square of a Number

In this program, we will learn to code the Java Program to Find Square of a Number. Let’s understand How to calculate square of a number in Java Programming Language.

We are going to solve this Java Program to Find Square of a Number using two approaches as follow

  • Using Math.pow() fuction
  • Using “*” operator

Now, let’s get straight into the code of the Java Program to Find Square of a Number.

Java Program to Find Square of a Number

Using Math.pow Function

Let’s see the code of the Java Program to Find Square of a Number using Math.pow() function.

import java.util.*;
import java.lang.*;

class Main
{
	public static void main (String[] args)
	{
		    Scanner sc = new Scanner(System.in);
		    System.out.println("Enter the number: ");
		    int num = sc.nextInt();
		    
		    //Calculating the square of the number
		    int sqr = (int) Math.pow(num,2);
		    
		    System.out.println("Square of the number "+num+" is "+sqr+" .");
		 
	}
}

Output

Enter the number: 7
Square of the number 7 is 49 .

How Does This Program Work ?

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number : ");
        int num = sc.nextInt();

In this program, we have taken the input of the number we want to calculate the square of using the Scanner class in Java.

		    //Calculating the square of the number
		    int sqr = (int) Math.pow(num,2);

Then, we have used the Math.pow() function in Java to calculate the square of the number. The Math.pow() function returns a double value, so, to store the number in int type we have explicitly typecasted it.

		    System.out.println("Square of the number "+num+" is "+sqr+" .");

Then, we displayed the result.

This is the Java Program to Find Square of a Number using Math.pow() function. Now, let’s see another approach using “*” operator.

Java Program to Find Square of a Number

Using “*” Operator

Let’s see the code of the Java Program to Find Square of a Number using “*” operator.

import java.util.*;
import java.lang.*;

class Main
{
	public static void main (String[] args)
	{
		    Scanner sc = new Scanner(System.in);
		    System.out.println("Enter the number: ");
		    int num = sc.nextInt();
		    
		    //Calculating the square of the number
		    int sqr = num * num;
		    
		    System.out.println("Square of the number "+num+" is "+sqr+" .");
		 
	}
}

Output

Enter the number: 18
Square of the number 18 is 324 .

This is the Java Program to Find Square of a Number using “*” operator.

Conclusion

I hope after going through this post, you understand the Java Program to Find Square of a Number. In this programming example, we have discussed two approaches to find the square of the number in Java Programming Language.
If you have any doubt regarding the topic, feel free to contact us in the comment section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *