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:
- Start
- Create an instance of the Scanner class.
- Declare a variable.
- Ask the user to initialize the variable.
- Call a user-defined method to find the square root of the number.
- Declare a temporary variable.
- Declare another variable to store the number/2 value.
- Use a do-while loop to calculate the square root.
- Calculate the square root of the number and return the value.
- Now, print the square root of the number.
- 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:
- Start
- Create an instance of the Scanner class.
- Declare a variable.
- Ask the user to initialize the variable.
- Use a pre-defined method to find the square root of the number.
- Use Math.pow() to calculate the square root of the number.
- Print the value of the square root of the number.
- 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