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:
New Features in Java 11
Hướng dẫn Java Design Pattern – Flyweight
Mệnh đề if-else trong java
Java Program to Implement Hash Trie
A Guide to the finalize Method in Java
The HttpMediaTypeNotAcceptableException in Spring MVC
Tìm hiểu về Web Service
Spring Boot - File Handling
Java 8 – Powerful Comparison with Lambdas
Marker Interface trong Java
Predicate trong Java 8
Java Program to Construct an Expression Tree for an Infix Expression
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Java Program to Implement Hash Tables
Java Web Services – JAX-WS – SOAP
Hướng dẫn Java Design Pattern – Mediator
LinkedHashSet trong Java hoạt động như thế nào?
Java Program to Perform Searching in a 2-Dimension K-D Tree
Remove All Occurrences of a Specific Value from a List
Comparing Two HashMaps in Java
Spring RequestMapping
Checking for Empty or Blank Strings in Java
Difference Between Wait and Sleep in Java
Java Program to Implement Find all Cross Edges in a Graph
Java Program to Find kth Largest Element in a Sequence
Fixing 401s with CORS Preflights and Spring Security
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Java Program to Implement Dijkstra’s Algorithm using Queue
Converting a Stack Trace to a String in Java
Introduction to Liquibase Rollback
DynamoDB in a Spring Boot Application Using Spring Data
How to Manually Authenticate User with Spring Security