Java program for a square root or a number in Java
How to write a Java program to find the square root of a number is a common Java programming exercise that many institutes use in their Java course along with Java program to print Fibonacci series and How to find Armstrong numbers in Java, which we have seen earlier. Java program for the square root is also a popular question during college semester exams and various programming tests.
If you are familiar with Java API then writing a Java program that can find the square root of a number using java.lang.Math class is not difficult. You just need to pass a double value and it returns a double which is the square root of the number you passed.
In the next section, we will see the complete code example of finding the square root of a number from the Java program. Another Java coding question which is very popular and related to programming exercises is writing Java program to find prime numbers, if you are going to appear in Java interviews then it’s worth looking.
How to find the Square root of a number in Java? Example
As I said it’s easy to calculate square root using java.lang.Math sqrt() function but things can be a tricky question if the interviewer will ask you to write your own sqrt() function during programming interviews.
Well its not that easy to write sqrt() function which can operate on double but one trick I use to remember the concept that square of the square root must be either less than or equal to the number, you can use that concept to write your own sqrt() method in any programming language including Java.
Barring Java interviews, I suggest programmers use standard JDK library or open-source library like Spring, Apache etc for such common tasks because of the quality of code and amount of testing done of those libraries.
Btw, a basic knowledge of data structure and algorithms is needed and if you need to brush up then do so. If you need a resource, I highly recommend checking out these Java data structure and algorithms courses on Udemy. It’s a hands-on course and covers all essential data structures.

Java program to find the square root of a number
Anyway, here we will see the Java program for finding square root using the API method,
package test;
import java.util.Scanner;
/**
*
* Java program to find the square root of a number in Java.
* This Java program example demonstrates using Math class
* sqrt() method to get the square root of a number in Java.
*
* @author java67
*/
public class SquareRoot{
public static void main(String args[]) {
//Used to get input number for which square root to find
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number to find square root in Java : ");
//getting input number from user to calculate square root
double square = scanner.nextDouble();
//getting the square root of a number in Java
double squareRoot = Math.sqrt(square);
//printing number and its square root in Java
System.out.printf("Square root of number: %f is : %f %n" , square, squareRoot);
}
}
Output:
Enter number to find square root in Java :
64
The square root of a number: 64.000000 is: 8.000000
This was our Java program to find the square root of a number in Java. It can find the square root of any floating-point number and you don’t need to only supply integers numbers. As I said use Java standard library methods to calculate square root but prepare with your own version of the square root function if going for any programming interview.
Pingback: JAVA TOP 50 PROGRAMMING QUESTIONS WITH SOLUTIONS - GRAD JOB OPENINGS