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 Optimal Paranthesization Using Dynamic Programming
Cài đặt và sử dụng Swagger UI
A Guide to Spring Boot Admin
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Exploring the Spring Boot TestRestTemplate
Java Program to Implement Selection Sort
Spring NoSuchBeanDefinitionException
Serve Static Resources with Spring
Connect through a Proxy
Java Program to Implement Bucket Sort
Java Program to Implement Fibonacci Heap
String Processing with Apache Commons Lang 3
Introduction to Spring Cloud OpenFeign
Java Program to Implement Bubble Sort
Spring Boot - Rest Controller Unit Test
Java Program to Implement Leftist Heap
Guide to @JsonFormat in Jackson
Java Program to Search for an Element in a Binary Search Tree
Convert Hex to ASCII in Java
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Java Program to Implement Direct Addressing Tables
A Quick Guide to Spring MVC Matrix Variables
Java List UnsupportedOperationException
Java Program to Implement LinkedHashMap API
Spring RestTemplate Error Handling
Custom Exception trong Java
Testing in Spring Boot
Java Program to Implement Unrolled Linked List
Quick Guide to java.lang.System
Introduction to Spring Cloud Stream
Hướng dẫn Java Design Pattern – Object Pool
Java Collections Interview Questions