Java Program to find out square root of a number

December 30, 2022, Learn eTutorial

How to calculate the square root using a java program?

First, we have to declare the class SqRoot. Then declare the integer variable num. Create an object of the scanner class as sc. Then, read the number from the user into the variable num. Call the java Math class function sqrt() to find out the square root of the number num. The sqrt() function will return the square root of the double value we passed into the function. Finally, display the result.

What is the sqrt() function in java?

The sqrt() is a built-in function in java that is used to find out the square root of the number. The Syntax of the function sqrt() is shown below.

public static double sqrt(double x)


  • x : is the number that the user given
  • Return value: this fucntion will return the square root value of the value we passed to the function


STEP 1: Declare the class SqRoot as public.

STEP 2: Open the main(), { Java program execution starts with the main() }

STEP 3: Declare the integer variable num.

STEP 4: Read the number from the user and add that to the variable num.

STEP 5: Call the function Math.sqrt(num) to find the root of the number and store it in the variable sr.

STEP 6: Display the result of the num as sr.

Java Source Code

                                          import java.util.Scanner;
public class SqRoot { 
public static void main(String[] args)  
 int num;
Scanner sc = new Scanner(;
System.out.println("Enter the Number: ");
num= sc.nextInt();
double sr = Math.sqrt(num);      
System.out.println("The Square Root of " +num+ " = "+sr); 


Enter the Number: 4
The Square Root of 4 = 2.0