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:
Retrieve User Information in Spring Security
Hướng dẫn Java Design Pattern – Service Locator
Java Program to Solve the 0-1 Knapsack Problem
Java Program to Implement Euclid GCD Algorithm
Abstract class và Interface trong Java
Java Program to Implement Meldable Heap
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Java Program to Implement RoleList API
Reading an HTTP Response Body as a String in Java
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Checking for Empty or Blank Strings in Java
Jackson Unmarshalling JSON with Unknown Properties
Java – Rename or Move a File
Getting Started with Stream Processing with Spring Cloud Data Flow
Migrating from JUnit 4 to JUnit 5
Java Program to Implement JobStateReasons API
Java Program to Implement Word Wrap Problem
New Stream Collectors in Java 9
Spring Boot Tutorial – Bootstrap a Simple Application
Exploring the Spring 5 WebFlux URL Matching
Java Program to Implement LinkedHashMap API
Jackson – Unmarshall to Collection/Array
Java Program to Implement Sorted List
Find the Registered Spring Security Filters
A Quick Guide to Spring MVC Matrix Variables
Spring Security Logout
Dockerizing a Spring Boot Application
Introduction to Java 8 Streams
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
Java Program to Implement Bubble Sort
Một số từ khóa trong Java