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 Implement Heap Sort Using Library Functions
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Java Program to Implement Gale Shapley Algorithm
Lập trình đa luồng với CompletableFuture trong Java 8
Guide to Apache Commons CircularFifoQueue
Java Program to Implement Triply Linked List
Java Program to Implement LinkedList API
Java Program to Generate All Subsets of a Given Set in the Gray Code Order
Java Program to Solve any Linear Equations
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph
HttpClient 4 – Send Custom Cookie
StringBuilder vs StringBuffer in Java
Derived Query Methods in Spring Data JPA Repositories
Practical Java Examples of the Big O Notation
Converting Between an Array and a Set in Java
Hướng dẫn sử dụng lớp Console trong java
Java Program to Implement Euler Circuit Problem
Java Program to Implement IdentityHashMap API
Migrating from JUnit 4 to JUnit 5
Java Program to Find the Longest Path in a DAG
Java Program to Implement the Monoalphabetic Cypher
Java Program to Create a Random Graph Using Random Edge Generation
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Java Program to Implement Dijkstra’s Algorithm using Set
Java Program to Generate All Possible Combinations of a Given List of Numbers
Giới thiệu Google Guice – Binding
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree
Mix plain text and HTML content in a mail
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Mapping Nested Values with Jackson
Spring WebClient and OAuth2 Support