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:
Initialize a HashMap in Java
Đồng bộ hóa các luồng trong Java
Java Program to Implement Stack
Spring Boot - File Handling
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
Lớp Collectors trong Java 8
Spring Boot - Twilio
Java Program to Implement Hash Tables with Double Hashing
Java Program to Construct an Expression Tree for an Prefix Expression
Lập trình hướng đối tượng (OOPs) trong java
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Java Program to Implement ArrayBlockingQueue API
Mệnh đề if-else trong java
Java Program to Implement Hash Tables with Linear Probing
Java Program to Implement Bit Array
An Example of Load Balancing with Zuul and Eureka
Debug a JavaMail Program
Collection trong java
Map Interface trong java
Từ khóa throw và throws trong Java
Java Program to Permute All Letters of an Input String
Java Program to Implement Ternary Search Tree
Servlet 3 Async Support with Spring MVC and Spring Security
New Stream Collectors in Java 9
Generate Spring Boot REST Client with Swagger
Java Program to Check whether Directed Graph is Connected using BFS
Exploring the Spring 5 WebFlux URL Matching
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Java Program to Find the GCD and LCM of two Numbers