This is a Java Program to Implement Horner Algorithm. Horner’s method is an efficient method for calculating polynomials.
Here is the source code of the Java Program to Implement Horner Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
/** ** Java Program to implement Horner Algorithm **/ import java.util.Scanner; public class Horner { private int sum; /** constructor **/ public Horner(int[] cof, int x) { sum = 0; calcSum(cof, x, cof.length - 1); display(); } /** Calculate sum **/ private void calcSum(int[] cof, int x, int N) { sum = cof[N] * x; for (int i = N - 1; i >= 1; i--) sum = (sum + cof[i]) * x; sum += cof[0]; } public void display() { System.out.println("Evaluated sum = "+ sum); } /** main method **/ public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Horner Algorithm Test\n"); System.out.println("Enter highest power"); int n = scan.nextInt(); int[] arr = new int[n + 1]; System.out.println("\nEnter "+ (n + 1) +" coefficients in increasing order"); for (int i = 0; i <= n; i++) arr[i] = scan.nextInt(); System.out.println("\nEnter x"); int x = scan.nextInt(); Horner h = new Horner(arr, x); } }
Output:
Horner Algorithm Test Enter highest power 5 Enter 6 coefficients in increasing order 1 2 3 4 5 6 Enter x 2 Evaluated sum = 321
Related posts:
Prevent Cross-Site Scripting (XSS) in a Spring Application
Implementing a Runnable vs Extending a Thread
Java Program to Implement Segment Tree
Java Program to Implement K Way Merge Algorithm
Java Program to Check the Connectivity of Graph Using BFS
Java Program to Implement Sorted Circularly Singly Linked List
Introduction to Spring MVC HandlerInterceptor
Java Program to Implement Extended Euclid Algorithm
Setting a Request Timeout for a Spring REST API
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Java 8 – Powerful Comparison with Lambdas
Java Program to Implement Radix Sort
Java equals() and hashCode() Contracts
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Java Program to Implement Hopcroft Algorithm
Spring Security and OpenID Connect
Intro to Inversion of Control and Dependency Injection with Spring
A Guide to JUnit 5 Extensions
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Java Program to Construct an Expression Tree for an Postfix Expression
Java Program to Implement Skip List
Creating Docker Images with Spring Boot
Java Program to Describe the Representation of Graph using Incidence Matrix
Spring WebFlux Filters
Java Program to Implement Insertion Sort
The Order of Tests in JUnit
Java Program to find the maximum subarray sum using Binary Search approach
Java Program to Implement Randomized Binary Search Tree
Adding Parameters to HttpClient Requests
Java – Random Long, Float, Integer and Double
Xử lý ngoại lệ trong Java (Exception Handling)