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:
Get the workstation name or IP
Use Liquibase to Safely Evolve Your Database Schema
Java Program to Implement Binary Search Tree
The Spring @Controller and @RestController Annotations
Spring MVC Async vs Spring WebFlux
Java Program to Represent Graph Using 2D Arrays
Java Program to Implement Queue using Two Stacks
An Intro to Spring Cloud Task
Guide to DelayQueue
Introduction to Liquibase Rollback
Compact Strings in Java 9
Spring Data MongoDB – Indexes, Annotations and Converters
Quick Guide to @RestClientTest in Spring Boot
Spring WebClient vs. RestTemplate
Java Program to Check whether Directed Graph is Connected using DFS
An Intro to Spring Cloud Vault
Performance Difference Between save() and saveAll() in Spring Data
Spring Cloud – Bootstrapping
Spring Cloud AWS – S3
Java Program to Implement Flood Fill Algorithm
Java Program to Implement Hash Tables Chaining with Binary Trees
An Example of Load Balancing with Zuul and Eureka
Từ khóa throw và throws trong Java
Java InputStream to String
Comparing Objects in Java
Java Program to Implement Adjacency Matrix
Spring Boot - Zuul Proxy Server and Routing
HTTP Authentification and CGI/Servlet
Java Deep Learning Essentials - Yusuke Sugomori
Spring Security Custom AuthenticationFailureHandler
Spring Autowiring of Generic Types
Guide to Dynamic Tests in Junit 5