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:
Lớp Arrarys trong Java (Arrays Utility Class)
Java Program to Implement Dijkstra’s Algorithm using Queue
Working with Kotlin and JPA
Java Program to Check whether Graph is a Bipartite using BFS
Hướng dẫn Java Design Pattern – Decorator
Java 8 StringJoiner
Java Program to implement Array Deque
Kết hợp Java Reflection và Java Annotations
Giới thiệu Google Guice – Aspect Oriented Programming (AOP)
Cài đặt và sử dụng Swagger UI
Collection trong java
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Weak References in Java
How to Remove the Last Character of a String?
Phân biệt JVM, JRE, JDK
Java Program to implement Dynamic Array
Sort a HashMap in Java
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
Java Program to Delete a Particular Node in a Tree Without Using Recursion
Serialize Only Fields that meet a Custom Criteria with Jackson
Spring Boot - Thymeleaf
Java Program to Check whether Undirected Graph is Connected using DFS
Base64 encoding và decoding trong Java 8
Spring’s RequestBody and ResponseBody Annotations
Java Program to Construct K-D Tree for 2 Dimensional Data
Removing all Nulls from a List in Java
Java Program to Implement Disjoint Sets
Control Structures in Java
Java Program to Find a Good Feedback Edge Set in a Graph
Guide to the Synchronized Keyword in Java
Write/Read cookies using HTTP and Read a file from the internet
HttpClient Connection Management