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:
Spring Data JPA Delete and Relationships
Servlet 3 Async Support with Spring MVC and Spring Security
Spring Boot - File Handling
Jackson Exceptions – Problems and Solutions
Most commonly used String methods in Java
Configuring a DataSource Programmatically in Spring Boot
Guide to Java Instrumentation
Java Program to Perform Insertion in a BST
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
Java 8 Stream API Analogies in Kotlin
Jackson – Bidirectional Relationships
Setting Up Swagger 2 with a Spring REST API
Spring Boot Change Context Path
Java Program to implement Priority Queue
Connect through a Proxy
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Hướng dẫn Java Design Pattern – Template Method
Java Program to Emulate N Dice Roller
Java Program to Check whether Graph is Biconnected
Java – Reader to InputStream
So sánh ArrayList và Vector trong Java
Java Program to Implement Gauss Seidel Method
Introduction to Spring MVC HandlerInterceptor
Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree
LIKE Queries in Spring JPA Repositories
Guava – Join and Split Collections
Default Password Encoder in Spring Security 5
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Mệnh đề Switch-case trong java
Converting Iterator to List
Introduction to the Functional Web Framework in Spring 5
Custom Thread Pools In Java 8 Parallel Streams