Signup/Sign In
PUBLISHED ON: MARCH 31, 2021

Java Program to Find Square Root of a Number

In this tutorial, we will learn how to find the square root of a number in java. The square root of a number is defined as the value which on multiplication gives the original number. But before moving forward if you are not familiar with the concept of loops in java, then do check the article on Loops in Java.

Input: Enter the number: 49

Output: The square root of the number is 7.0

The above problem can be solved in the following ways:

Approach 1: Using a User-defined method

Approach 2: Using a Pre-defined Method

Let us look at each of these methods separately.

Program 1: Java Program to Find the Square Root of a Number

In this program, we will learn how to find the square root of a number in java without using a pre-defined method. Here, we will use the below logic to find the square root of a number.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare a variable.
  4. Ask the user to initialize the variable.
  5. Call a user-defined method to find the square root of the number.
  6. Declare a temporary variable.
  7. Declare another variable to store the number/2 value.
  8. Use a do-while loop to calculate the square root.
  9. Calculate the square root of the number and return the value.
  10. Now, print the square root of the number.
  11. Stop

Below is the code for the same.

//Java Program to Calculate the square root of a number
import java.util.Scanner;  
public class Main   
{  
    public static void main(String[] args)    
    {
        //Take input from the user
        //Create an instance of the Scanner class
        Scanner sc = new Scanner(System.in); 
        System.out.println("Enter a number: ");  
        int n = sc.nextInt();  
        System.out.println("The square root of "+ n+ " is: "+squareRoot(n));  
    }  
    //user-defined method to find the square root  
    public static double squareRoot(int num)   
    {  
        //temporary variable  
        double temp;  
        double sqrtroot=num/2;  
        do   
        {  
            temp=sqrtroot;  
            sqrtroot=(temp+(num/temp))/2;  
        }   
        while((temp-sqrtroot)!= 0);  
        return sqrtroot;  
   }  
}  

Ca
Enter a number: 45
The square root of 45 is: 6.708203932499369

Program 2: Java Program to Find the Square Root of a Number

In this program, we will learn how to find the square root of a number in java by using a pre-defined method.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare a variable.
  4. Ask the user to initialize the variable.
  5. Use a pre-defined method to find the square root of the number.
  6. Use Math.pow() to calculate the square root of the number.
  7. Print the value of the square root of the number.
  8. Stop

Below is the code for the same.

//Java Program to Calculate the square root of a number
import java.util.Scanner;  
public class Main   
{  
    public static void main(String[] args)    
    {
        //Take input from the user
        //Create an instance of the Scanner class
        Scanner sc = new Scanner(System.in); 
        System.out.println("Enter a number: ");  
        int num = sc.nextInt();  
        Double squareroot = Math.pow(num, 0.5);
        System.out.println("The Square Root of the Given Number  " + num + "  =  " + squareroot);
 
   }  
}  


Enter a number: 36
The Square Root of the Given Number 36 = 6.0



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.