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:
Servlet 3 Async Support with Spring MVC and Spring Security
Custom Error Pages with Spring MVC
Apache Commons Collections BidiMap
Concurrent Test Execution in Spring 5
Java Program to Generate All Possible Combinations of a Given List of Numbers
Comparing Long Values in Java
Comparing Objects in Java
How to Count Duplicate Elements in Arraylist
Shuffling Collections In Java
Java Program to Implement SynchronosQueue API
XML Serialization and Deserialization with Jackson
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Java Program to Implement Pagoda
Life Cycle of a Thread in Java
Spring Boot Integration Testing with Embedded MongoDB
Java Program to Implement ConcurrentLinkedQueue API
Consuming RESTful Web Services
ArrayList trong java
Java 9 Stream API Improvements
Running Spring Boot Applications With Minikube
Getting the Size of an Iterable in Java
Inject Parameters into JUnit Jupiter Unit Tests
Java Program to Find kth Largest Element in a Sequence
Java Program to Implement EnumMap API
Java Program to Check if it is a Sparse Matrix
Java Program to Implement Find all Cross Edges in a Graph
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Guide to the Synchronized Keyword in Java
How to Get All Spring-Managed Beans?
What is a POJO Class?
Receive email using IMAP
Exploring the Spring Boot TestRestTemplate