How to Square a Number in Java?

You are currently viewing How to Square a Number in Java?

In this article, You are going to learn the topic of How to Square a Number in Java program? Initially, We will talk about a square of a number and how to do a task using the java program? Let’s move on to this tutorial.

A square of a number is nothing but the given input integer is multiplying by the same integer itself.

For Example, If we can choose the input integer as “5” then the square of a number will be like “5*5=25”.

Here, We can use the two methods to square a number using java program. Those are described below:

  1. Math.pow() function,
  2. Square() function.

How to Square a Number in Java

Here, The Math.pow() function will use to perform a task in the below program.

import java.util.*;
public class Square {
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int num;
     
        System.out.print("Enter an integer number: ");
        num=sc.nextInt();
     
        System.out.println("Square of "+ num + " is: "+ Math.pow(num, 2));
        
    }
} 

Explanation

Initially, We have to import java.util.* class into this program. Secondly, The new scanner() function will help to scan the input number and show the square of an input number.

After executing this program, The program asks you to enter the input integer and click the enter button to see the output. The output will be calculated by the Math.pow() function. Finally, The output will show on the screen using the system.out.printIn() function.

Output

The output will be shown below for your reference.

Enter an integer number: 8
Square of 8 is: 64.0
Read Also : Input date Format Using HTML

How to Square a Number in Java

Now, We can use the square() function to do a task in the below program.

import java.util.Scanner;
public class SquareNum {
	public static void main(String [] args)
	{
		Scanner sc = new Scanner(System.in);
		System.out.print("Please enter a number: ");
		int num = sc.nextInt();
		System.out.println("Your squared number is: " + square(num));
	}
	public static int square(int num) 
	{
		return num * num;
	}
}

Explanation

Firstly, We need to import the java.util.Scanner class into this program. Otherwise, The execution of both programs will be the same. Here, We will use the square() function rather than Math.pow() function to calculate the square of a number. Finally, We will get and see the output using the system.out.printIn() function.

Output

The output of “how to square a number in java” will be given below for your reference.

Please enter a number: 16
Your squared number is: 256

Final Words

I hope this Java article helps you to do “How to Square a Number in Java?”. If you have any doubts about this topic then please let us know via the comment section. We will help you. Share this article with other Java developers. Thank You!

Manoj S

Hi, I'm Manoj working as a part-time blogger in Coding Diksha. Here, I post about programming to help developers.

Leave a Reply