0 0
WP Ad Inserter plugin
Read Time:7 Minute, 5 Second

Table of Contents

If you have a number and wish to find its square root, multiply the number by its component plus itself. In this article, we’ll show you how to use the Java programming language to find the square root of any number. The precise syntax and coding for calculating square roots are covered along with examples since a Math.sqrt() is utilized for this purpose.

Exploring square as well as the square root

A number multiplied by itself forms the square. To put it another way, the result of multiplying an integer by itself is known as the numbers’ square. In mathematics, a number’s square is represented as,

The square of a given number n is equivalent to n*n. For instance, 5*5 = 25 is the square of 5.

The square root is the square’s exact opposite. A number’s square root, n, is the number that results in n when multiplied by itself. The square root of a number is represented mathematically as,

// The Square Root of a given number n is √ n

Now that you know what a number’s square and square root are, let’s examine various Java methods for calculating them.

SUGGESTED READ

How do you square a number In Java?

In Java, there are two ways to square a number:

  • Multiplication of the given number by itself.
  • Using Math.pow() function

Approach 1: Multiply an integer by itself to square it.

Here is a Java program to multiply a number by itself to square it.

package Codeunderscored;
import java.util.Scanner; public class CodeSquare { public static void main(String args[]) { Double numVal; Scanner scann= new Scanner(System.in); System.out.print("Kindly enter a number between 1-100: "); numVal=scann .nextDouble(); Double squaredVal = numVal*numVal; System.out.println("The values Square :"+ numVal + " is: "+ squaredVal); }
}

Approach 2: Use the Math.pow algorithm to square an integer

The Math.pow method to square a number is called by the following Java program.

package Codeunderscored;
import java.util.Scanner;
import java.lang.Math; public class CodeSquare {
public static void main(String args[]) {
Double numVal;
Scanner scann= new Scanner(System.in);
System.out.print("Enter a number: ");
numVal = scann.nextDouble();
Double squaredVal = Math.pow(numVal, 2);
System.out.println(" "+ numVal + " Square' is: "+ squaredVal);
}
}

How to use Java to Find the Square Root of a Number

In Java, there are several approaches to finding the square root of a given number. Let’s look at some of them.

Method 1: Use java.lang.Math.sqrt() in a Java program to find the square root of a number

The Syntax is as follows:

SUGGESTED READ

public static double sqrt(double x)

Parameter: The value x for which the square root is to be returned.

Return: The square root of the input supplied to this method is returned.

  • This method will return the square root of x if parameter x is a positive double number.
  • This method will return NaN if x is NaN or less than zero.
  • This method will return positive Infinity if parameter x is positive Infinity.
  • This method will return Zero with the same sign whether x is positive or negative.

Method 2: The square root of a number can be found using a Java program using the java.lang.Math.pow()

To determine the square root of an integer, we can use the formula as illustrated in the code block below.

package Codeunderscored;
import java.util.Scanner; public class CodeSquareRoot { public static void main(String[] args) { Double numVal; Scanner scann= new Scanner(System.in); System.out.print("Enter a number: "); numVal = scann.nextDouble(); Double squareRootVal = Math.pow(numVal, 0.5); System.out.println("The Square of a Given Number " + numVal + " = " + squareRootVal); }
}

Method 3: Using a Java program to calculate a number’s square root devoid of any built-in functionality

The implementation of this logic is as follows:

package Codeunderscored; public class CodeSquareRoot
{ public static double square(double numVal){ double t; double squarerootVal = numVal / 2; do { t = squarerootVal; squarerootVal = (t + ( numVal / t)) / 2; } while ((t - squarerootVal) != 0); return squarerootVal; } public static void main(String[] args) { double numVal = 9; double rootVal; rootVal = square(numVal); System.out.println("The provided number is : "+numVal); System.out.println("The Square Root of the number is: "+rootVal); }
}

Java instructions for finding a number’s square root

With the help of the java.util.Scanner class and the java.lang.Math class, Java offers users a very streamlined and simple method for entering values using the keyboard. Java.util.Scanner is imported to utilize this object.

SUGGESTED READ

import java.util.Scanner; import java.lang.Math; // The calculations are done in a public class called "SqurareRoot," which we have created: public class SquareRoot { ……… }

Additionally, a scanner object is developed to scan user input:

Scanner scann=new Scanner(System.in);

You can now type and print whatever is currently displayed on the screen.

System.out.println("The resulting Square Root in Java ");

You can ask the user for input by entering the following text:

int newVal=scann .nextInt();

We have utilized an integer data type to store the input in the variable x in the statement above. Then, using an if-else conditional statement, we need to build code that is used to determine the square root. The condition mentioned above demonstrates that the square root would not be an actual number if a variable’s value were less than zero. Therefore, it is advised to use a positive integer in this example:

if(newVal<0)

The condition mentioned above demonstrates that the square root would not be an actual number if a variable’s value were less than zero. Therefore, it is advised to use a positive integer in this example:

SUGGESTED READ

else { // Use the java.lang.Math class in the calculation of the square root. double sqrtVal=Math.sqrt(newVal);
System.out.println("The Square root of: " + newVal +" is :"+ sqrtVal);

The otherwise statement states that a function is used to calculate the square root of an integer if it is higher than zero:

double sqrtVal=Math.sqrt(newVal);

Because a square root can also be an infraction, we have utilized a double data type in this case. The outcome is saved in a new variable called “sqrtVal.” In light of this, the full if-else conditional expression is provided below.

if(newVal<0) {
System.out.println("Error! A negative number's square root is not a valid number."); }
else { double sqrtVal=Math.sqrt(newVal);
System.out.println(" " + newVal +" ' Square root is "+ sqrtVal); }

We will now demonstrate the entire code that we created to calculate the square root:

import java.util.Scanner;
import java.lang.Math; public class CodeForSquareRoot { public static void main(String[] args) { //Create a Scanner object to receive data. Scanner scann=new Scanner(System.in);
System.out.println("Java Square Root. Example 1");
System.out.println("Please enter an integer (whole number) "); int newVal=scann.nextInt(); //If newVal is a negative integer, display an error message. if(newVal<0) {
System.out.println("Error! A negative number's square root is not a valid number."); } else { double sqrtVal=Math.sqrt(newVal);
System.out.println(" " + newVal +"'s Square root is: "+ sqrtVal); } }
}

Example: Using Math.sqrt

package Codeunderscored;
public class CodeSquareRoot
{ public static void main(String args[]) { double numOne = 100; System.out.println(Math.sqrt(numOne)); // Input positive value, Output square root of x double numTwo = -81.00; System.out.println(Math.sqrt(numTwo)); // Input negative value, Output NaN double numThree = 0.0/0; // Input NaN, Output NaN System.out.println(Math.sqrt(numThree)); double numFour = 1.0/0; // Input positive infinity, Output positive infinity System.out.println(Math.sqrt(numFour)); double numFive = 0.0; // Input positive Zero, Output positive zero System.out.println(Math.sqrt(numFive)); }
}

Conclusion

We have computed the square root of diverse numbers in this article using the Java programming language. Since we calculated the square root of a positive number as a negative number, the Math.sqrt(x) function used for this purpose will not have a true result. The double-typed value that is supplied to the java.lang.Math.sqrt() function as an argument is returned as its square root. The argument must be NaN or negative for the result to be NaN. Positive infinity is the result of the argument is positive infinity. The result will be the same as the argument if the argument is positive zero or negative zero.

Source: https://www.codeunderscored.com/calculate-the-square-root-in-java/

When Do You Use CSS Columns? Previous post When Do You Use CSS Columns?
Bubble Sort in Java with examples Next post Bubble Sort in Java with examples

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.