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:
Guide to the Volatile Keyword in Java
Hướng dẫn Java Design Pattern – Chain of Responsibility
Java Program to Implement LinkedHashSet API
Java Program to Solve Knapsack Problem Using Dynamic Programming
A Guide to JPA with Spring
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 – Try with Resources
Marker Interface trong Java
Spring MVC Custom Validation
Spring Webflux and CORS
Java Program to Optimize Wire Length in Electrical Circuit
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Quick Guide on Loading Initial Data with Spring Boot
Java Program to implement Dynamic Array
Java Multi-line String
Configure a Spring Boot Web Application
Notify User of Login From New Device or Location
Lớp Collections trong Java (Collections Utility Class)
Encode a String to UTF-8 in Java
Java – Reader to InputStream
Setting Up Swagger 2 with a Spring REST API
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Java Program to Implement Floyd Cycle Algorithm
Java Program to Implement Variable length array
Spring WebFlux Filters
Hướng dẫn Java Design Pattern – Transfer Object
Java 14 Record Keyword
Guide to WeakHashMap in Java
wait() and notify() Methods in Java
Java Program to Implement Best-First Search
Spring Boot - Service Components