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 Perform Polygon Containment Test
Hướng dẫn Java Design Pattern – Null Object
Mapping Nested Values with Jackson
Java Program to Perform Deletion in a BST
Java String to InputStream
A Guide to EnumMap
Convert Hex to ASCII in Java
Hướng dẫn Java Design Pattern – Adapter
Introduction to Spring Cloud OpenFeign
Circular Dependencies in Spring
Connect through a Proxy
Spring Boot - Building RESTful Web Services
Introduction to the Java NIO Selector
Merging Two Maps with Java 8
Hướng dẫn Java Design Pattern – Observer
Tổng quan về ngôn ngữ lập trình java
Quick Guide to Spring MVC with Velocity
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
Java Streams vs Vavr Streams
Java Program to Implement the Checksum Method for Small String Messages and Detect
Hướng dẫn Java Design Pattern – Factory Method
Lớp TreeMap trong Java
Immutable ArrayList in Java
Overflow and Underflow in Java
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Initialize a HashMap in Java
Guide to Spring Cloud Kubernetes
Java Program to Compute Cross Product of Two Vectors
Java Program to Implement Dijkstra’s Algorithm using Queue
HashSet trong Java hoạt động như thế nào?
Iterating over Enum Values in Java
Using a Spring Cloud App Starter