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:
Spring Autowiring of Generic Types
Spring Cloud AWS – Messaging Support
Convert Hex to ASCII in Java
Java InputStream to String
The Difference Between Collection.stream().forEach() and Collection.forEach()
Java Program to Implement Sorted Doubly Linked List
Wiring in Spring: @Autowired, @Resource and @Inject
Java Program to Implement vector
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Java Program to Implement Attribute API
Java – Write to File
Truyền giá trị và tham chiếu trong java
Comparing Dates in Java
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
OAuth2 Remember Me with Refresh Token
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
Cài đặt và sử dụng Swagger UI
Java Program to Implement ScapeGoat Tree
Giới thiệu Google Guice – Binding
Checked and Unchecked Exceptions in Java
Hướng dẫn Java Design Pattern – Intercepting Filter
Auditing with JPA, Hibernate, and Spring Data JPA
Spring Security Remember Me
Java Program to Solve the Fractional Knapsack Problem
Hướng dẫn Java Design Pattern – Interpreter
Java Program to Implement Fibonacci Heap
Check If Two Lists are Equal in Java
How to Break from Java Stream forEach
Java Program to Implement CopyOnWriteArraySet API
Introduction to the Java NIO Selector
How to Remove the Last Character of a String?