Java Program to Implement Nth Root Algorithm

This is a Java Program to Implement Nth Root Algorithm. This program is used to calculate n th root of a number x.

Here is the source code of the Java Program to Implement Nth Root Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/**
 ** Java Program to implement Nth Root Algorithm
 **/
 
import java.util.Scanner;
 
/** Class NthRoot **/
public class NthRoot
{
    public double nthroot(int n, double x) 
    {
        return nthroot(n, x, .0001);
    }
    public double nthroot(int n, double x, double p) 
    {
        if(x < 0) 
        {
            System.err.println("Negative!");
            return -1;
        }
        if(x == 0) 
            return 0;
        double x1 = x;
        double x2 = x / n;  
        while (Math.abs(x1 - x2) > p) 
        {
            x1 = x2;
            x2 = ((n - 1.0) * x2 + x / Math.pow(x2, n - 1.0)) / n;
        }
        return x2;
    }
    /** Main **/
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nth Root Algorithm Test\n");
        System.out.println("Enter n and x");
        int n = scan.nextInt();
        double x = scan.nextInt();
        NthRoot nr = new NthRoot();
        double root = nr.nthroot(n, x);
        System.out.println("\nRoot = "+ root);
    }    
}

Output:

Nth Root Algorithm Test
 
Enter n and x
2
3
 
Root = 1.7320508100147274

Related posts:

Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Implementing a Binary Tree in Java
The Modulo Operator in Java
Java Program to Implement Interpolation Search Algorithm
Java – Rename or Move a File
Java Program to Implement LinkedBlockingQueue API
Java Program to Implement Heap
Using a Spring Cloud App Starter
How To Serialize and Deserialize Enums with Jackson
Java – Delete a File
How to Get All Dates Between Two Dates?
Hamcrest Collections Cookbook
Java Program to Implement Binomial Tree
Command-Line Arguments in Java
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Convert String to Byte Array and Reverse in Java
Spring Boot - Creating Docker Image
Hướng dẫn Java Design Pattern – Bridge
Bootstrapping Hibernate 5 with Spring
Java Program to Implement Bresenham Line Algorithm
Java Program to Implement Hash Tables with Linear Probing
Send email with authentication
JWT – Token-based Authentication trong Jersey 2.x
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Guide to the Java Clock Class
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Custom Error Pages with Spring MVC
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Returning Custom Status Codes from Spring Controllers
Spring Security 5 – OAuth2 Login
Apache Commons Collections MapUtils