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:

Tìm hiểu về xác thực và phân quyền trong ứng dụng
Java Program to Find Transpose of a Graph Matrix
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Java Program to find the number of occurrences of a given number using Binary Search approach
Using the Map.Entry Java Class
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
Java 8 Collectors toMap
Hướng dẫn sử dụng Lớp FilePermission trong java
Java – Reader to Byte Array
Java Program to Find Maximum Element in an Array using Binary Search
Hướng dẫn Java Design Pattern – Prototype
Serialize Only Fields that meet a Custom Criteria with Jackson
Java Collections Interview Questions
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Limiting Query Results with JPA and Spring Data JPA
Java Program to Implement Sieve Of Eratosthenes
Encode/Decode to/from Base64
Java Program to Implement ConcurrentSkipListMap API
Bootstrap a Web Application with Spring 5
Custom Cascading in Spring Data MongoDB
Spring Autowiring of Generic Types
Java Program to Implement LinkedBlockingQueue API
Java Program to Implement CountMinSketch
Java Multi-line String
Java Program to Check Multiplicability of Two Matrices
Java Program to Implement ArrayList API
A Guide to the ResourceBundle
Marker Interface trong Java
Hướng dẫn Java Design Pattern – Memento
Receive email by java client
Java Program to Check Whether a Given Point is in a Given Polygon