How to Square a Number in Java?: Math.pow() Method, Examples & More

How to Square a Number in Java: This Article will define how to square a given number in Java and give working code examples that highlight this key math function. Math in Java Yes, it might be confusing, but there is math in programming. Joking separately, you’ll detect that great mathematical concepts are used when evolving applications. If you’ve ever speculated when you’d use progressive math and algebra in the real world, here is your answer.

This article will cover the square function. This can be explained in two ways: we can represent it as multiplying a number by itself or as raising a number to the second power. Once you’ve learned the functions in this lesson, you’ll be able to go for squaring numbers and lift them to other powers.

How to Square a Number in Java

If you need to square a number, a very simple explanation is to just multiply the number by itself. After all, 7² is really 7 * 7. In this occurrence, it’s totally sufficient to multiply a number by itself, although there is another tool that will achieve this purpose. Below is the basic Java code to square the number 7.

How to Square a Number in Java

How to Square a Number in Java

  1. package MyPackage;
  2. import java.util.Scanner;
  3. public class Square1
  4. {
  5.     public static void main(String args[])
  6. {
  7. Double num;
  8. Scanner sc= new Scanner(System.in);
  9. System.out.print(“Enter a number: “);
  10. num=sc.nextDouble(); 
  11.  Double square = num*num;
  12. System.out.println(“Square of “+ num + ” is: “+ square);
  13. }
  14. }

The output of this code is below, which says, as you can see:

Output

  1. Enter a number: 7
  2. Square of 10.0 is: 49.0

We’ll talk about data categories in a couple of moments, but you’ll notice that we made x into a double, even though it’s a 7. When we start using other powers and squaring huger numbers, it’s good to have room to raise. If we’re absolutely sure that the number will be an integer, we can disclose them as the long data type instead.

Squaring a Number In Java- Using Math.pow() Method

While the previous code is legal and compiles, there’s a delivered math function we can use. In fact, it’s a good idea to use this function, because it works for raising numbers to any chosen power. Java’s Math.pow() function takes two parameters: the number being modified, and the power by which you are raising it. In our case here, it’ll be 2.

  1. package MyPackage;
  2. import java.util.Scanner;
  3. import java.lang.Math;
  4. public class Square2 {
  5. public static void main(String args[])
  6. Double num;
  7. Scanner sc= new Scanner(System.in);
  8. System.out.print(“Enter a number: “);
  9. num = sc.nextDouble();
  10. Double square = Math.pow(num, 2);
  11. System.out.println(“Square of “+ num + ” is: “+ square);
  12. }
  13. }

Output

  1. Enter a number: 11
  2. Square of 11.0 is: 121.0

How to Find Square Root of a Number in Java

There are many ways to find square root a given number in Java. Let’s see a few of those.

java.lang.Math.sqrt() method

Syntax

public static double sqrt(double x)

  • Parameter: x is the value whose square root is to be returned.
  • Return: This method returns the square root value of the argument passed to it.
  • If parameter x is positive double value, this method will return the square root of x
  • When x is NaN or less than zero, this method will return NaN
  • If parameter x is positive infinity, this method will return positive Infinity
  • When x is positive or negative zero, this method will return the result as Zero with the same sign

Code

  1. package MyPackage;
  2. public class SquareRoot2 {
  3.   public static void main(String args[])
  4.     {
  5.         double a = 100;
  6.    
  7.         System.out.println(Math.sqrt(a));
  8.         // Input positive value, Output square root of x 
  9.    
  10.         double b = -81.00;
  11.    
  12.         System.out.println(Math.sqrt(b));
  13.         // Input negative value, Output NaN 
  14.    
  15.         double c = 0.0/0;
  16.         // Input NaN, Output NaN 
  17.    
  18.         System.out.println(Math.sqrt(c));
  19.    
  20.         double d = 1.0/0; 
  21.         // Input positive infinity, Output positive infinity  
  22.    
  23.         System.out.println(Math.sqrt(d));
  24.          
  25.         double e = 0.0;
  26.         // Input positive Zero, Output positive zero 
  27.          
  28.         System.out.println(Math.sqrt(e));
  29.     }
  30.          
  31. }

Output

  1. 10.0
  2. NaN
  3. NaN
  4. Infinity
  5. 0.0

 java.lang.Math.pow() method

We can use the concept √number = number½ to find the square root of a number.

Code

  1. package MyPackage;
  2. import java.util.Scanner;
  3. public class SquareRoot1 {
  4. public static void main(String[] args)
  5. {
  6. Double num;
  7. Scanner sc= new Scanner(System.in);
  8. System.out.print(“Enter a number: “);
  9. num = sc.nextDouble();
  10. Double squareroot = Math.pow(num, 0.5);
  11. System.out.println(“The Square of a Given Number ” + num + ” = ” + squareroot);
  12. }
  13. }

Output

  1. Enter a number: 81
  2. The Square of a Given Number 81.0 = 9.0

FAQ

How Do You Find The Square Of A Number?
To Find The Square Of A Number, Multiply The Number By Itself. For Example, If You’re Trying To Find The Square Of 5, You Would Multiply 5 By 5 And Get 25, Which Is The Square. To Learn How To Square Fractions, Scroll Down!
How Do You Do Exponents In Java?
Type The Following Anywhere In The Document To Find An Exponent: Double Result = Math. Pow(Number, Exponent); Replace “Number” With The Base Value And “Exponent” With The Exponent It Is To Be Raised To.
How Do You Determine If A Number Is A Perfect Square In Java?
The Following Java Program Checks Whether A Given Number Is A Perfect Square Number Or Not. We Take The Square Root Of The Passed In Number And Then Convert It Into An Integer. Then We Take A Square Of The Value To See Whether It Is Same As The Number Given. If They Are Same, We Got A Perfect Square Number!