Kodekraftt
/Blog
/Java Program To Find The Square Root Of Any Number Using The Newtonraphson Method
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Java Program to Find the Square Root of Any Number Using the Newton–Raphson Method

Learn how to calculate the square root of any number in Java using the Newton–Raphson numerical method. This efficient algorithm provides fast and accurate results without using Math.sqrt().

Java Program to Find the Square Root Using the Newton–Raphson Method

Finding the square root of a number without using Math.sqrt() is a great way to understand numerical methods. One of the fastest and most accurate techniques is the Newton–Raphson method.

This method starts with an initial guess and iteratively improves it using the formula:


xₙ₊₁ = (xₙ + number / xₙ) / 2

With each iteration, the guess becomes more accurate, making this algorithm extremely fast for large values.

How the Newton–Raphson Method Works

  • Start with an initial guess (we use half of the input number).
  • Improve the guess using the Newton formula.
  • Repeat until the difference between two successive guesses is very small.

Java Program:


import java.util.Scanner;

public class SquareRootNewton {

    public static double squareRoot(double num) {
        if (num < 0) {
            throw new IllegalArgumentException("Square root of a negative number is not real.");
        }

        double guess = num / 2.0;

        // Keep improving until change becomes very small
        while (Math.abs(guess * guess - num) > 0.00001) {
            guess = (guess + num / guess) / 2.0;
        }

        return guess;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        double num = sc.nextDouble();

        double result = squareRoot(num);

        System.out.printf("Square root of %.5f is: %.5f", num, result);
    }
}

Example Output:


Enter a number: 49
Square root of 49.00000 is: 7.00000

Enter a number: 2
Square root of 2.00000 is: 1.41421

This result is extremely close to the real value because the Newton–Raphson method converges very quickly.

Example (Step-by-Step for 25)

  • Initial Guess = 25 / 2 = 12.5
  • Next Guess = (12.5 + 25 / 12.5) / 2 = 7.25
  • Next Guess = (7.25 + 25 / 7.25) / 2 = 5.349
  • Next Guess = (5.349 + 25 / 5.349) / 2 = 5.011
  • Next Guess = (5.011 + 25 / 5.011) / 2 = 5.0000 (accurate)

Within just a few iterations, we reach the exact square root!

Practice Challenges

  1. Modify the program so the user can enter the precision value (e.g., 0.01, 0.0001).
  2. Rewrite the program to calculate cube root using the Newton–Raphson method.
  3. Handle negative inputs by returning complex square roots instead of throwing an error.

The Newton–Raphson method is one of the fastest ways to compute roots and is widely used in numerical algorithms and engineering computations.

You might also like

Check if Two Strings Are Rotations of Each Other in Java — Definition, Examples and Program

Learn how to check whether two strings are rotations of each other in Java using logical string comparison without using complex libraries.

Find the Shortest Word in a Sentence in Java — Definition, Examples and Program

Learn how to find the shortest word in a sentence in Java using a simple character-by-character approach without using split() or advanced string methods.

Abundant Number in Java — Definition, Examples and Program

Learn what an abundant number is in Java with definition, examples, and a clean program to check whether a number is abundant or not.

Concatenate Two Strings in Java Without Using + Operator — Examples and Program

Learn how to concatenate two strings in Java without using the + operator. This blog explains the logic with examples, a Java program, and practice challenges.

Check if a String is Empty or Null in Java — Definition, Examples and Program

Learn how to check whether a string is null or empty in Java. This blog explains the difference, common mistakes, examples, and a safe Java program with practice challenges.

Convert String to Uppercase in Java Without Using String Library

Learn how to convert a string to uppercase in Java without using built-in String methods. This blog explains the ASCII-based logic with examples, a Java program, and practice challenges.